views:

88

answers:

2

Hi,

During destruction of the derived class object, i first hit the derived class destructor and then the base class destructor (which is as expected). But i was curious to find out - at what point does the functions of the derived class go out of scope (are destroyed).

Does it happen as soon as the control leaves the derived class destructor and goes toward the base? Or does it happen once we done with the base class destructor also.

Thanks

+8  A: 

Once the destructor of the most derived class finishes, the dynamic type of the object can be considered that of the next less-derived-type. That is, a call to a virtual method in the base destructor will find that the final overrider at that point in time is at base level. (The opposite occurs during construction)

struct base {
   base() { std::cout << type() << std::endl; }
   virtual ~base() { std::cout << type() << std::endl; }
   virtual std::string type() const {
      return "base";
   }
};
struct derived : base {
   virtual std::string type() const {
      return "derived";
   }
};
int main() {
   base *p = new derived;
   std::cout << p->type() << std::endl;
   delete p;
}
// output:
// base
// derived
// base
David Rodríguez - dribeas
Nice answer. Does that mean that 'actually' you're not supposed to call virtual functions from the destructor?
xtofl
Yes, please have a look at : http://www.artima.com/cppsource/nevercall.html
Manav Sharma
Thanks David. Sorry for the long text following:But suppose, I spawned a thread from the base class constructor and destroy it in the base class destructor. This thread calls a virtual method implemented in the derived class.Now before i destroy the thread in the base class destructor, the thread triggers and calls the virtual method of the derived class. But we have already gone past derived class destructor. Would this thread still find the definition of the virtual method implemented in derived class? or is the virtual method implementation of derived class gone out of scope.
Manav Sharma
@xtofl: I wouldn't say "not supposed to"; the behaviour is well defined, unless the function is pure virtual. But it could get a bit confusing if you're not careful.
Mike Seymour
@Manav: In that case you'll get undefined behaviour, as the destructor "changes" the dynamic type while the other thread still has access to it. You need to make sure the thread has stopped (or at least won't call virtual functions or access the derived class's members again) before the derived class's destructor completes.
Mike Seymour
Yes, as Mike mentioned, it's the case for pure virtual functions. another great article by Scott Meyers: http://www.artima.com/cppsource/pure_virtual.html
Manav Sharma
Right Mike, Thanks!
Manav Sharma
+4  A: 

Functions don't get destroyed.

Virtual functions however get their entry in the v-table erased as soon as the derived destructor finishes so you can't call derived virtual functions from the base d'tor.

shoosh
Thanks Shoosh. That's another good point about v-table that you mentioned.
Manav Sharma