Sign in to follow this  
daviddiligent

deep question for C++

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
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
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

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