Sign in to follow this  
daviddiligent

deep question for C++

Recommended Posts

daviddiligent    122
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
SiCrane    11839
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
SiCrane    11839
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
Gage64    1235
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
Rattenhirn    3114
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
ToohrVyk    1596
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
nbertoa    1022
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
nbertoa    1022
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this