473,372 Members | 840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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 4059
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

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

Similar topics

11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
21
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
3
by: Dmitry Prokoptsev | last post by:
Hello, I need to write a class for exceptions which can be thrown, caught, stored and thrown once again. I have written the following code: --- begin --- #include <string> class Exception...
4
by: Eric | last post by:
I was wondering what people thought about the information found at: http://g.oswego.edu/dl/mood/C++AsIDL.html Specifically, I am interested in the following recommendation: ---- Since...
7
by: arnaudk | last post by:
How come it's possible to have a base class with a pure virtual destructor and nonetheless one has to implement the destructor? Doesn't pure virtual mean that there is no implementation of that...
14
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
7
by: Tonni Tielens | last post by:
I'm trying to create a pure virtual class describing an interface. Normally, when I do this I make the destructor pure virtual so that, even if there are no members in the class, it cannot be...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.