Prevent instantiation of base class with no pure virtual methods

Started by
19 comments, last by Quasimojo 10 years, 10 months ago

I've been toying with a component-based system for a game, and I'm struggling a bit with the design of my component base class. My components will basically be data buckets with little to no functionality. The attributes every component will have include EntityID and TypeID. I would like to create a base component class with those two properties from which all specialized components will inherit. How can I prevent instantiation of the the base class with no methods to be made purely virtual?


enum ComponentType {
    ATTR_HP
    , ATTR_AC
    , ATTR_STR
    , ATTR_INT
};

class Component {
    public:
        Component(long entityID, ComponentType compType) : EntityID(entityID), TypeID(compType) {}
        long EnityID;
        int TypeID;
};

class CharAttribute : public Component {
    public:
        int Value;
        int ValueMin;
        int ValueMax;
};

I suppose I could make the EntityID and TypeID members private and then make the accessors pure virtual, but then I would pretty much have to repeat the same code in the implementation for every subclass.

What's the best way to make method-less base classes abstract?

Advertisement

If you are going to inherit from Component, it should have a virtual destructor. Can you add "=0" to the destructor, and then define it to be empty anyway.

EDIT: Clicky.

Make a destructor a pure virtual.

Sorry, I didn't see Alvaro's comment when I commented.

Though in this case you might consider instead of the type id being a member variable, making the type id a virtual function that the child classes implement. This would also be a good candidate for a pure virtual function.

If you are going to inherit from Component, it should have a virtual destructor. Can you add "=0" to the destructor, and then define it to be empty anyway.

EDIT: Clicky.

I had considered that, though it will still require


~DerivedComponent() {}

in every derived class. I guess that's about as simple as it's going to get, though. Thanks!

Just make the constructor of the base class protected...

EDIT: You need a constructor for each derived class anyway, since the base class constructor is not a default constructor. As long as you don't provide any public methods to construct a base class inside the base class or any derived class (e.g. a static function returning a new Component), using a protected constructor is the easiest way and doesn't require any virtual functions at all.

You will probably find you may need a virtual destructor in the base class eventually, but as you say that would require all derived classes to implement a do nothing destructor if they don't need to do anything.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

I should point out that I don't quite understand why you would want a base class with no virtual functions. If you are not going to use polymorphism here, what's the point of using inheritance?

You'd have to have an EntityID and a TypeID in every component then. You could use an aggregate object as a member of each component but then you have to use member.entityID instead of just entityID, so it's just more convenient syntax.

Of course if you need to call any members of the derived class through a pointer or reference to the base class you need to do a cast, which is ugly and a virtual function would be better instead.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Just make the constructor of the base class protected...

EDIT: You need a constructor for each derived class anyway, since the base class constructor is not a default constructor. As long as you don't provide any public methods to construct a base class inside the base class or any derived class (e.g. a static function returning a new Component), using a protected constructor is the easiest way and doesn't require any virtual functions at all.

You will probably find you may need a virtual destructor in the base class eventually, but as you say that would require all derived classes to implement a do nothing constructor destructor if they don't need to do anything.

Oh. I assumed the constructor definition in the base class would be used by all derived classes. Again, I'd hate to have to repeat that same constructor definition in all sub-classes. Also, I corrected what I believed to be a typo near the end of your reply. Please correct me if I'm wrong. I'm just trying to keep up, here.

I should point out that I don't quite understand why you would want a base class with no virtual functions. If you are not going to use polymorphism here, what's the point of using inheritance?

Every derived class will have the data members, EntityID and TypeID. Beyond that, they will each have additional data members specific to their purposes. The only functionality I've determined they will all share is the assignment of EntityID and TypeID upon instantiation, but it doesn't sound like I can accomplish that from the base class definition.

If there is a better way to accomplish this, I'd appreciate it if someone could modify my design above to illustrate.

This topic is closed to new replies.

Advertisement