ra*******@gmail.com wrote:
hello,
i want to know that is virtual constructors possible? if not why?
also does virtual destructors functions are often used in C++
programming.
A virtual function dispatches a function based on the actual type of
object being pointed at/referred to. Until the object exists (after the
ctor finishes execution) there IS no object whose type can be
determined.
If you ever destroy an object of a derived class via a pointer to a
base class, the destructor needs to be virtual. For example, this code
gives undefined behavior:
class base {};
class derived : public base {};
// ...
base *b = new derived;
// ...
delete b;
To get defined behavior, you need code more like this:
class base {
virtual ~base() {}
};
class derived : public base {};
// ...
base *b = new derived;
// ...
delete b;
In cases like this, the destructor is often defined only to make it
virtual, so it's fairly common for the destructor's body to be empty.
--
Later,
Jerry.
The universe is a figment of its own imagination.