c++ - Why in destructors is the virtual table set back to that level? -


following question - pure virtual call in destructor of derived class - tried code check syntax , discovered sucessive destructors called, call relevant virtual functions. consider code:

class base { public: virtual void method() = 0; };  class derived :  public base { public: ~derived() {     method(); }  virtual void method() {     cout << "d"; } };  class doubled : public derived { public:  ~doubled() {     method(); }  virtual void method() {     cout << "dd"; } };  int main(array<system::string ^> ^args) {     doubled d;     doubled e;     return 0; } 

as expected, object gets destructed, calls correct method (eg first derived , the second derived).

ouput: dd d

my question is, why work? since not meant call virtual functions in c'tor/d'tor, why virtual table "unwind" correctly.

eg, can see why derived 1 works, state virtual function pointer table in when started. why, when derived's destructor called, table correctly set point @ classes implementation of method.

why not leave it, or if being nice, set value null.

virtual table doesn't modified @ run time after initial setup @ object creation. on implementations, virtual table shall created per class basis.

in example, when doubled object destroyed, calls method function in doubled class, because, doubled part of object not yet destroyed completely. vtable of doubled class has entry method function point method in class overridden(in last level of inheritance)

once doubled destroyed, object type of type derived. call has go method in vtable of class derived. hence behavior.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -