user6245072 user6245072 - 29 days ago 8
C++ Question

How should I name these classes?

I have two different group of classes which inherit from two different base classes (

Value
and
Token
).

In both groups I have classes that I would simply call
String
,
Number
,
Name
, if only their name didn't clash with the class name from the other group.

So basically I need a way to distinguish the class
String
that inherits from
Value
from the class
String
that inherits from
Token
, and I have no idea on how to do this.

I can't put them in the namespace of their base class because thy inherit from the same class they are in and the compiler gives me an "invalid use of incomplete type" error. How should I resolve this?

Answer

The simple solution is to name them StringValue and StringToken.

You can't define them inside their base classes like:

struct Value
{
    struct String : Value {};
};

But you can define them and the base class inside a namespace:

namespace Value
{
    struct Base {};
    struct String:Base {};
}

and then refer to Value::Base or Value::String.

While I am very fond of namespaces, in this particular case, I would tend to the StringValue solution.

(Note: I have used struct in the examples because it saves all the messing around with public. For real code, you probably want to use class.)

Comments