Reputation: 717
Sometimes in my code appear a class (e.g. AI) that is interface for a lot of subclasses, whose names all contains base class' name (e.g. AIFighter, AIMage, AIThief...). The only logical thing to do is to organise it:
struct AI
{
virtual void think()=0;
};
namespace AI {
struct Fighter : public AI
{
void think()
{
//attack something
}
};
struct Mage : public AI
{
void think()
{
//burn something
}
};
//...
}
int main()
{
AI * ai = new AI::Fighter; // It's my target
}
But neither it nor any other ideas I tried (typedefs, inline namespaces, using) are working. Is it possible? It would be useful not only in inheritance but also in any group built around some central class.
Upvotes: 2
Views: 145
Reputation: 38825
I'm not going to solve your problem with a code example. The reason is, you're clearly violating the reason we do things a particular way.
You have a namespace AI
, and a struct AI
, which unfathomably is outside of the AI namespace.
The answer to your question, in any reasonable terms, is to rename one or the other. This is just simply bad design.
Upvotes: 0