Question about inherited constructor

Started by
1 comment, last by xorjesus 21 years, 2 months ago
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]
I study day and night, memorizing the game.
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.
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''
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]

This topic is closed to new replies.

Advertisement