Jump to content
  • Advertisement
Sign in to follow this  
daviddiligent

deep question for C++

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

I dont know the answer of this qustion, but it might be easy for you to understand. Say if I have a class A which has a virtual function run() (no matter public or private). Then I have class B: pulbic A What kind of problem or what will happen if run() is invoked in B's constructor like pulbic B() { run(); // or A::run() or B::run() , any difference? } Cheers

Share this post


Link to post
Share on other sites
Advertisement
If run is invoked in the B constructor, B::run() will execute, provided there is a definition for B::run(). Otherwise A::run() will execute, unless it was pure virtual without a definition, in which case Bad Things™ will happen. This will be the case even if B's constructor is called as part of a further derived class' constructor.

Share this post


Link to post
Share on other sites
Usually your program will crash. However, it is officially undefined behavior, which means that anything can happen.

Share this post


Link to post
Share on other sites
Quote:
yeap, i just want to know what kind of bad things will happen?


The program might crash with a message saying "trying to call an abstract virtual function" (happened to me once), but it's not guarantied.

Share this post


Link to post
Share on other sites
In your example, most likely, nothing bad would happen.

However, consider class C, which is derived from B.
C overrides run() with some funky code
Cs constructor calls Bs constructor, which calls the virtual run().
Now Cs version of run() will be called, which then can access parts of C that are not properly constructed, because Cs constructor is not finished yet.

Generally it's not advisable to invoke virtual methods in constructors or destructors. Always explicitely state, which version of the virtual method you want to run (i.e. A::run() or B::run() or C::run()).



Share this post


Link to post
Share on other sites
Quote:
Original post by Rattenhirn
Now Cs version of run() will be called, which then can access parts of C that are not properly constructed, because Cs constructor is not finished yet.


No. B's version of run() would be called, precisely to avoid the issue you mention.


Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVykNo. B's version of run() would be called, precisely to avoid the issue you mention.


You're right of course, my bad!

Relevant part of the C++ faq

So, in conclusion, the only problem is, that some people might expect that C::run() is invoked, and are confused that it isn't... ;)

Share this post


Link to post
Share on other sites
The book "Thinking in C++" explains this very detailed.
If you provided a definition for B::run(), then B::run() will be executed.
If tou don't provided a definition for B::run(), then A::run() will be called.

I recommend all to read Thinking in C++, is a really good book and deep.

Share this post


Link to post
Share on other sites
Things that can happen:

If you provided a definition for B::run(), and you has a member object in class B (X_Name) that is derived from the same base class than B, and you invoke X_Name obj.run(), in the constructor, you can initialize inexistents members from B in X_Name.

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!