473,320 Members | 1,821 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,320 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 4056
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.