Hi,
I have defined pure virtual destructor in base class. As far as I know pure virtual function makes us to redefine the same function in derived classes also. and also we can not create a objects of class which contains pure virtual functions but we can create pointer or reference of the same.
My problem is I have defined a pure virtual function in base class, so I have to redefine the same in derived class. When I did the same compiler gives me the following error.
error: declaration of ~base as member of derived
where ~base is base class destructor and derived is derived class name. This I tried in Linux machine. Is it possible to redefine the base class destructor in derived class?
Can you tell me weather my understanding is correct or I am trying to do the wrong thing.
And also Please tell me where this virtual concept is useful in any real world scenario.
Thanks,
Manjunath
Hi,
unlike other member functions of a derived class, the destructor of the base class is called at the end of the destructor of a derived class. Naturally, the compiler will give an error if the base class's destructor was not defined, even though it's declared (if it's not declared, the compiler can generates an empty).
Therefore if you declare a destructor pure virtual, you still have to implement a body somewhere, even if it is empty. But then, I don't see why declaring it pure virtual would be useful.
For your other question about the virtual concept: it is useful for base classes, classes that will be derived from, because then you get run-time polymorphism. That is, calling a function of a derived class through a pointer to the base class will call the derived class's function, not the base.
On the other hand, I think a pure virtual function is useful for an interface class. With a pure virtual destructor you declare that all derived classes
have to implement a destructor.
I hope this answers your questions!
P.s.: if you google for "Pure Virtual destructor", the second and first hits are useful (in this order).