468,512 Members | 1,426 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Pure Virtual destructor

72
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
Feb 28 '08 #1
2 3774
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).
Feb 28 '08 #2
manjuks
72
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).
Hi,

Thanks for your information. its cleared my doubts a lot.

Thanks,
Manjunath
Feb 28 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by santosh | last post: by
37 posts views Thread by WittyGuy | last post: by
6 posts views Thread by pakis | last post: by
21 posts views Thread by sks | last post: by
3 posts views Thread by Dmitry Prokoptsev | last post: by
4 posts views Thread by Eric | last post: by
7 posts views Thread by Tonni Tielens | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.