Jump to content

  • Log In with Google      Sign In   
  • Create Account


Prevent instantiation of base class with no pure virtual methods


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
20 replies to this topic

#1 Quasimojo   Members   -  Reputation: 220

Like
0Likes
Like

Posted 17 June 2013 - 08:18 PM

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?

 



Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 11709

Like
4Likes
Like

Posted 17 June 2013 - 08:23 PM

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.


Edited by Álvaro, 17 June 2013 - 08:25 PM.


#3 Crusable   Members   -  Reputation: 592

Like
0Likes
Like

Posted 17 June 2013 - 08:26 PM

Make a destructor a pure virtual. 


"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"

"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"

 

 


#4 Crusable   Members   -  Reputation: 592

Like
0Likes
Like

Posted 17 June 2013 - 08:27 PM

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


"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"

"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"

 

 


#5 SiCrane   Moderators   -  Reputation: 9338

Like
0Likes
Like

Posted 17 June 2013 - 08:32 PM

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.



#6 Quasimojo   Members   -  Reputation: 220

Like
0Likes
Like

Posted 17 June 2013 - 08:40 PM

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!



#7 Paradigm Shifter   Crossbones+   -  Reputation: 5094

Like
1Likes
Like

Posted 17 June 2013 - 08:50 PM

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.


Edited by Paradigm Shifter, 17 June 2013 - 09:59 PM.

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

#8 Álvaro   Crossbones+   -  Reputation: 11709

Like
0Likes
Like

Posted 17 June 2013 - 09:30 PM

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?



#9 Paradigm Shifter   Crossbones+   -  Reputation: 5094

Like
0Likes
Like

Posted 17 June 2013 - 09:36 PM

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

#10 Quasimojo   Members   -  Reputation: 220

Like
0Likes
Like

Posted 17 June 2013 - 09:57 PM

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.



#11 Paradigm Shifter   Crossbones+   -  Reputation: 5094

Like
0Likes
Like

Posted 17 June 2013 - 10:02 PM

Yup, I dun' did a typo in my post, corrected now.

 

You still need to call the base class constructor, so all your derived classes need a constructor. How else could you construct a derived class? If you have a default constructor for the base class that would be OK but you haven't.


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

#12 Quasimojo   Members   -  Reputation: 220

Like
0Likes
Like

Posted 17 June 2013 - 10:07 PM


You still need to call the base class constructor, so all your derived classes need a constructor. How else could you construct a derived class? If you have a default constructor for the base class that would be OK but you haven't.

 

What do I need to do to the constructor I've declared in the base class to cause it to be used as the default constructor for derived classes?



#13 swiftcoder   Senior Moderators   -  Reputation: 9510

Like
0Likes
Like

Posted 17 June 2013 - 10:14 PM


What do I need to do to the constructor I've declared in the base class to cause it to be used as the default constructor for derived classes?

A default constructor has no parameters.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#14 Paradigm Shifter   Crossbones+   -  Reputation: 5094

Like
1Likes
Like

Posted 17 June 2013 - 10:26 PM

 


What do I need to do to the constructor I've declared in the base class to cause it to be used as the default constructor for derived classes?

A default constructor has no parameters.

 

 

Or it has all optional parameters. As long as it can be called without any arguments it is a default constructor.

 

You can't use a default constructor in this case because you need to use different IDs for each derived class.


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

#15 Álvaro   Crossbones+   -  Reputation: 11709

Like
0Likes
Like

Posted 17 June 2013 - 11:43 PM

 

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.

 

 

I see. So you would be happy to have a struct called Identification that contains EntityID and TypeID, and have every class have a member of that type, except you would then have to say object.identification.EntityID instead of simply object.EntityID.

 

I strongly recommend sacrificing a few bytes and structuring your code in the simpler way.



#16 frob   Moderators   -  Reputation: 18373

Like
1Likes
Like

Posted 18 June 2013 - 12:07 AM

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.
In that case you are not specifying an implementation, you are specifying an interface.


However, for all the component systems I've worked on over the years, it is almost certain that your base class will pick up all kinds of wonderful functionality. This is a good thing. All components may be serialized. All components may get updated. All components may have signals and slots. All components may have visualization methods, at the very least to handle component names and properties during debugging. All components in your game will do this, that, and something else. It should absolutely be made into an abstract base class for your purposes. (That includes the virtual destructor, which is basically mandatory on any class meant for inheritance.)
Check out my personal indie blog at bryanwagstaff.com.

#17 Quasimojo   Members   -  Reputation: 220

Like
0Likes
Like

Posted 18 June 2013 - 02:43 PM

Just to make sure I understand correctly, there is no way to declare a constructor that assigns values to data members in an abstract base class that will provide that default constructor functionality in all derived classes, correct?  Therefore, all derived classes will have to have the same constructor declared:

class DerivedComponent1{
    public:
        DerivedComponent1(long entityID, ComponentType compType) : EntityID(entityID), TypeID(compType) {}
       ~DerivedComponent1();    // Because it's just good practice

        long EnityID;
        ComponentType TypeID;
};

class DerivedComponent2{
    public:
        DerivedComponent2(long entityID, ComponentType compType) : EntityID(entityID), TypeID(compType) {}
       ~DerivedComponent2();

        long EnityID;
        ComponentType TypeID;
};

Edited by Quasimojo, 18 June 2013 - 02:43 PM.


#18 swiftcoder   Senior Moderators   -  Reputation: 9510

Like
0Likes
Like

Posted 18 June 2013 - 03:03 PM

Therefore, all derived classes will have to have the same constructor declared

Not quite. The base class needs to define and initialise the data members, and the derived classes' constructors should call the base class' constructor:
class Component {
public:
    Component(long entityID, ComponentType compType) : entityID(entityID), compType(compType) {}

    long entityID;
    ComponentType compType;
};

class DerivedComponent : public Component {
public:
    Derived(long entityID, ComponentType compType) : Component(entityID, compType) {}
};

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#19 phil_t   Crossbones+   -  Reputation: 3109

Like
0Likes
Like

Posted 18 June 2013 - 03:04 PM

In my component system I also used to have entityId and typeId. However, after some refactoring I did, I realized I didn't need them at all anymore! Any code that is operating on a Component already knows what type it is (or doesn't care), and knows which entity it came from. So I ended up removing them (saving 8 bytes per component). This will really depend on your particular implementation though (I see more reason for requiring entityId than typeId).

 

In my case I still have a Component base class. But it only contains pure virtual functions (mainly for serialization, which subclasses are required to implement, and also for a Reset() method, since I pool and re-use components).

 


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.

 

That saves memory, at the cost of requiring a virtual method call every time you need to get the type id. Not saying that's good or bad, but that's something to think about.



#20 swiftcoder   Senior Moderators   -  Reputation: 9510

Like
1Likes
Like

Posted 18 June 2013 - 03:13 PM

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.

That saves memory, at the cost of requiring a virtual method call every time you need to get the type id. Not saying that's good or bad, but that's something to think about.

Type id should be the same for all instances of a given component. We're getting out of For Beginners territory here, but it might be a good candidate to make a template parameter of the base class.

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS