• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Quasimojo

Prevent instantiation of base class with no pure virtual methods

20 posts in this topic

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?

 

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites


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?

0

Share this post


Link to post
Share on other sites


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.

0

Share this post


Link to post
Share on other sites

 


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.

1

Share this post


Link to post
Share on other sites

 

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.

0

Share this post


Link to post
Share on other sites

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.)
1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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) {}
};
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.
1

Share this post


Link to post
Share on other sites


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).

 

I think I see how that would work.  You wouldn't typically need to traverse all components of a given type by type id, when you could accomplish the same through the component's respective system.

 

With regard to serializing the entity, my first thought would be that all relevant systems would need to be queried for the given entity.  Is there a more efficient way to go about it?

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0