Jump to content
  • Advertisement
Sign in to follow this  
Nairou

Inheritance of structs without member functions

This topic is 2434 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

This question is likely a "what does the C++ standard say" thing, but my Google searching hasn't given me the answer I'm looking for.

I know that when you have classes, and you have one class inherit from another class, you get into the world of virtual function tables, since the code needs to figure out which class contains the function you're trying to call.

But what about inheritance between structs that only contain data? For example, if you have a widget struct, and then you want a specialized version of that struct that has a few extra variables, but you still want to be able to pass its original data to functions that handle widgets. Is there any overhead when there is only data involved in the inheritance? Is the specialized widget still a simple struct (in terms of memory layout) with both data combined, or is the original widget data stored separate from the new data?

Share this post


Link to post
Share on other sites
Advertisement
I can't quote the standard on this, but the de facto reality is that if you code something like this:

struct Base
{
int member;
bool flag;
};

struct Derived : Base
{
float stuff;
};


In memory you'll end up thusly (for a Derived):

int
bool
float

Of course, subject to padding/alignment/etc.

And if you start doing multiple inheritance everything becomes implementation-defined AFAIK.

Share this post


Link to post
Share on other sites
It does depend on what kind of inheritance you use. If you do virtual inheritance then extra junk will be added in even if you don't have any functions.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!