Jump to content
  • Advertisement
Sign in to follow this  
The C modest god

Virtual c'tor

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

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.

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
> 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.

Share this post


Link to post
Share on other sites
It would be easier and faster just to put print statements in the base and derived classes to figure out this behavior, FYI.

Share this post


Link to post
Share on other sites
Also aswell as factories & abstract factories you can apply virtual constructor idiom its really simple, you can see it here

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!