Question about inherited constructor
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]
The only thing allowed in an initializer list is members of the class and base class constructors. While
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.
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?
Update GameDev.net system time campaign:
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''
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement