Hi
I want to know how to initialize a member constructor AFTER the base class has been constructed.
eg;
class A
{
A();
}
class B
{
A
B();
}
A::A()
{
//need something from parent B here
}
B::B()
:A() <- this initizializes before B is constructed
{
} :A() <- this is wot i want, but its illegal syntax
Anyone know?
cheers
Stu
You can''t do that. C++ requires that all an object''s member objects be completely constructed before the constructor for the object is called. The best you can do is use an assignment operator or some other member function on the member object inside the parent object''s constructor.
Easy: empty the member's default constructor, then in the encapsulating class's consructor call the other constructor explicitly. class C { public: C() {} C(int i_) : i(i_) {} protected: int i; };
class A { public: A(int i) /*: C() called here*/ { DoSomeStuff(&i); c.C(i); } protected: C c; };
Just make sure your default is empty, and/or don't call it at all, but I'm not sure.
[edited by - temp_ie_cant_thinkof_name on January 25, 2004 4:48:04 PM]
quote:Original post by SiCrane temp_ie_cant_thinkof_name: that class A constructor does not contain legal C++ code. The "c.C(i)" expression is pure garbage.
Indeed. Instead, try new (&c) C(i);. Of course, this is evil.
"Sneftel is correct, if rather vulgar." --Flarelocke
With your permission, GameDev.net uses analytics cookies to understand how people use the platform.
You can accept analytics or continue with necessary cookies only.
Learn more