Sign in to follow this  

Making everything a subclass

This topic is 4660 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been writing all my C++ classes lately as subclasses of another class (Object, which just has a virtual destructor and nothing else) under the theory that I might someday want to make a pointer-to-Object that could actually point to any of my classes. I'm having a really hard time coming up with a scenario where I'd actually do that, though. So: Is it good form to do what I've been doing, or is it just needless complexity?

Share this post


Link to post
Share on other sites
In principle adding a virtual destructor will just force all classes to have a vtable and add extra storage for them.

It's likely that all classes which are "entities" in the game are going to want to be subclasses of a single base class.

The purpose of inheritance is to have a "core behaviour" which can be extended or overriden by the derived classes.

If the core object has no methods or member variables, that is pretty pointless as it'll be a "nothing" type item.

Also it's extremely likely that you'll have small classes with hardly any data that you never want to override any of the methods on - 3d vector for example. If you pointlessly extend your "does nothing" base class for your "3d vector" class, you'll add a useless vtable to every instance (which could create a significant memory overhead).

There are also likely to be some big "do lots of things" classes like GameManager, ResourceManager etc, which won't have any use for inheritance, instead they'll just get used to encapsulate a load of data and behaviour together.

So don't create a pointless base class for no reason - only for entities which share some common behaviour or data.

Mark

Share this post


Link to post
Share on other sites
Its not bad design pattern, but it is bad to design without pre-planning.

Having somewhat generic root class that is derived by all other is seen in game logic before (Unreal is the first to expose that, but I may easily be wrong). The bad part is that your Object class has nothing except destructor, so its not usable at all. When you instantiate a class to base class pointer type, you get no functionality from the child class (I assume you know that), and as such, your pointer is pretty much non valuable. On the other hand, if you're going to cast it to child class, why bothering at superclassing at all?

My advice is that you should construct your software on a class-per-solution basis, and then, when you find similarities between problems and program code, refactor. Its the way I'm doing my projects, and its working for me.

Share this post


Link to post
Share on other sites
Quote:
Original post by dark_696
The bad part is that your Object class has nothing except destructor, so its not usable at all.


Although i do agree that an Object type is really pretty useless type it does have one advantage in C++, i do have an Object type that is used to implement reference semantics for user-defined types that should have reference semantics rather than value. It maintains an embedded reference counting system so i don't get the very small overhead you get from non-intrusive reference counted smart pointers, i've overloaded operators new/delete that forward allocation to a small object allocator for small objects.

It has been really useful how-ever i rarely pass around (smart) pointers to type "Object" its just a clumsy type [smile].

Share this post


Link to post
Share on other sites

This topic is 4660 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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