Thursday, August 2, 2007

Calling Destructor & Virtual table c++

IF you really know how virtual functions work,it is really fun. Here is such an example(for fun i wrote a function fun  )

class base
public :
   virtual void fun()
        cout <"base fun";
class deriv: public base
    void fun()
         cout << "deriv fun";
   ~ deriv(){}

very simple two classes..
We know virtual funtions are initlaized in constructor and removed to their old base versions in the destructor.

Here is One more proof .
Try these if you are interested , otherwise go to

base *p = new deiv();

the outputs will be first

deriv fun and
base fun

Because when we call p->~base() it will replaces the virtual pointer with the base version of fun. Thats why the base::fun get called.
We can't call constructor explicity as we did with destructor.
 Ok , I am stopping now.

No comments: