473,657 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pure Virtual destructor

72 New Member
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 4081
Harinezumi
32 New Member
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 New Member
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
4349
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 it inline. Like this.
37
4155
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
3475
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
2557
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
3
4588
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 { public:
4
1861
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 interface classes cannot be directly instantiated, yet serve as virtual base classes for implementations, the constructors should take no arguments and should be listed as protected. Also, for similar
7
3693
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 method in the base class and the implementation is left entirely up to derived classes? class Base { public: Base() {} virtual ~Base() = 0; // pure virtual destructor } Base::~Base() // <-- Linktime error if {} // this...
14
2742
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
7345
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 instantiated. The difference now is that I'm making a generic interface with template arguments. Template classes should be defined in the header file, but it is not allowed for a destructor's definition to be in the class definition if the...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.