Jump to content
  • Advertisement
Sign in to follow this  

Inheritance question

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

Quick question regarding inheritance here... If I have 2 classes class Foo1 { public: int FooData; } class Foo2 { public: int FooData2; } and another one that inherits from both class DerivedFoo : public Foo1, public Foo2 { public: int FooData; DerivedFoo() : FooData(5) {} } what exactly is the statement DerivedFoo() : FooData(5) {} doing? My guess is that DerivedFoo is setting it's own variable (FooData) to 5. My buddy says that since FooData is public in Foo1, that it essentially makes those 2 variables the same. What are your guys' thoughts? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Well, you can't use constructor initializer lists to initialize parent variables, only variables in that class, it would use the DerivedFoo variable. If you were setting the value in the actual body of the constructer I would guess it would still use the child version, though I don't know if that is standardized somewhere.

I would just avoid re-using variable names.

If you remove int FooData; from DerivedFoo, it will generate a compiler error.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!