Virtual c'tor

Started by
6 comments, last by snk_kid 19 years, 8 months ago
Suppos I have this code: class A { public: virtual A (); }; class B: public A { public: B (); }; B obj; For obj, will the A c'tor be called automatically within the call to B c'tor? or do I need to call A c'tor explicitly.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Advertisement
Constructors are implicitly static. Hence, they can't be virtual.
Have a look at something like a "class factory" for constructing types based on information at runtime.
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Quote:Original post by The C modest god
For obj, will the A c'tor be called automatically within the call to B c'tor?


yes, always. you can't declare it virtual but child class constructors always get called. it's just the destructor that needs to be virtual.

-me
Quote:Original post by Palidine
Quote:Original post by The C modest god
For obj, will the A c'tor be called automatically within the call to B c'tor?


yes, always. you can't declare it virtual but child class constructors always get called. it's just the destructor that needs to be virtual.

-me

You mean parent c'tor will be called?
I call B(the child) c'tor and A(the parent) c'tor will be automatically called?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
> You mean parent c'tor will be called?
> I call B(the child) c'tor and A(the parent) c'tor will
> be automatically called?

Yes.
Ok thanks for the help.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
It would be easier and faster just to put print statements in the base and derived classes to figure out this behavior, FYI.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Also aswell as factories & abstract factories you can apply virtual constructor idiom its really simple, you can see it here

This topic is closed to new replies.

Advertisement