can any body please say me about virtual constructor and destructor
can any body please say me about virtual constructor and destructor
"me about virtual constructor and destructor"
No, really, what do you want to know ? I'm not going to write a tutorial for you but I can help you on a specific question though.
Constructors can't be virtual.
Destructors of a base class should almost always be virtual to ensure that the proper destructor is called. Eg.
In the code above, if the destructor in Base is virtual, then the delete would call Derived::~Derived(); but if the destructor was not virtual, it would call Base::~Base(). Since pBase is actually a pointer to Derived, the wrong destructor would be called and bad things could happen.Code:Base* pBase = new Derived; ... delete pBase;
This is true, but there is an idiom known as the "virtual copy constructor". In this code;
the Clone() member function is referred to as a "virtual copy constructor". It must be supplied by every class derived from Base, and is used to create a copy of an object (assuming that both base and derived classes have properly functioning copy constructors).Code:class Base { public: virtual ~X() {}; // virtual destructor virtual Base *Clone() const = 0; }; class Derived: public Base { public: virtual Base *Clone() const {return new Derived(*this);}; } int main() { Base *x, *y; x = new Derived; y = x->Clone(); // x and y both point at an instance of Derived, and *y is a copy of *x }
a *virtual* constructor is not really a constructor, it's a member function that returns a pointer to the class it's declared in.
virtual constructor is really an oxymoron, they don't really exist. though its purpose may seem to be a constructor, it's just a member function.