Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

xorjesus

Question about inherited constructor

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

class a { protected: int var; }; //right class b : public a { b() { var = 10; } // why do I have to assign? }; // error class c : public a { c():var(10) {} // WHY CAN'T I INITIALIZED }; could this just be ghetto msvc? If not could osmeone explain.. [edited by - xorjesus on January 23, 2003 1:45:10 AM]

Share this post


Link to post
Share on other sites
Advertisement
The only thing allowed in an initializer list is members of the class and base class constructors. While var is an inherited variable, it isn''t a direct member of class c and thus you can''t initialize it in an initializer list.

It should be the job of the base class to initialize its members anyway. If derived classes need to override this, then the base class can either provide an overloaded constructor, or the derived class can set each variable manually.

Share this post


Link to post
Share on other sites
Think for yourself, what would the following code do (if it where legal). How would mFoo be constructed?

  
class Foo
{
public:
Foo(int i)
{
// Do someting...

}
};
class Bar
{
public:
Bar()
: mFoo(42)
{
}
private:
Foo mFoo;
};
class Baz : public Bar
{
public:
Baz()
: mFoo(47)
{
}
};




Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''

Share this post


Link to post
Share on other sites

  • 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!