471,092 Members | 1,731 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Pure virtual base class destructor

424 256MB
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?
Expand|Select|Wrap|Line Numbers
  1. class Base
  2. {
  3. public:
  4.     Base() {}
  5.     virtual ~Base() = 0; // pure virtual destructor
  6. }
  7. Base::~Base() // <-- Linktime error if 
  8. {}            //      this implementation is omitted!
  9.  
In fact, it seems I can provide an implementaiton of any pure virtual base class method without error although the linker doesn't complain if I do not, like it does with the destructor.
May 16 '08 #1
7 3521
RRick
463 Expert 256MB
This looks like one of those 'gotchas' where one rule takes precedence over another rule. The pure abstract does not "normally" need the base class to define the method.

Your problem is that you are playing around with the mandatory methods that every class needs. For example, every class needs a destructor and at least one constructor. If you don't define the method, then the compiler will make one for you. If you declare the method yourself (as you did with the pure destructor), then you also have to supply a definition. The compiler won't do this for you.
May 16 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
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?
That's not what pure virtual means.

Here is the correct meaning: The pure virtual method cannot be called using an instance of the class.

That doesnt't say anything about an implementation. In fact, a pure virtual function can have an implementation and you can call it using an instance of another class, like the derived class:

Expand|Select|Wrap|Line Numbers
  1. class Base
  2. {
  3.     public:
  4.        virtual void Hello() = 0;
  5. };
  6. void Base::Hello()
  7. {
  8.      cout << "arnaudk" << endl;
  9. }
  10.  
  11. clase Derived :public Base
  12. {
  13.     public:
  14.         void Hello();
  15. };
  16.  
  17. void Derived::Hello()
  18. {
  19.      this->Base::Hello();    //call base class function
  20. }
  21.  
In the case of your pure virtual destructor, that doesn't exist since you cannot override a class destructor from an other class. I expect some compilers will producd an error if you do this.

The virtual destructor in the base class tells the compiler that when you delete a base pointer, that you want the derived destructors called. If you don't do this, when you delete the base pointer only the base destructor is called.

There is simply no way for the compiler to figure this out on the basis of a base pointer alone.
May 17 '08 #3
arnaudk
424 256MB
Thank you. Previously, I was under the impression that one could not have instances of a class with pure virtual methods. That clears up my confusion!
May 17 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
Previously, I was under the impression that one could not have instances of a class with pure virtual methods. That clears up my confusion!
You have the correct impression: You cannot have instances of a class with pure virtual methods.

You have to read what I said carefully. What I said was you cannot call a pure virtual method with an instance of the class. But that you can call this pure virtual method with an instance of a derived class.

What happens is, the compiler does not trust you to behave. It's a very suspicious piece of software. It knows, sure as heck, that if you get to create an instance of a class with a pure virtual method, that later on you will you that object to call the pure virtual function. So, the compiler won't let you create the object in the first place.
May 18 '08 #5
arnaudk
424 256MB
Indeed, I soon discovered I can not instantiate abstract class as I previously thought.

But if I can not create instances of an abstract base class, how can I access the implementation of its pure virtual methods? I mean, the compiler complains if I don't provide an implementation of those pure virtual methods in derived classes, and those will override any base class implementation when called.
It seems to me the only time you ever see the base class implementation of a pure virtual base class method is when a pure virtual destructor is called.
May 18 '08 #6
weaknessforcats
9,208 Expert Mod 8TB
Look at nmy Post #3.

The base class implements the pure virtual method.

The derived class overrides this with its own method. However, that pure virtual method is still a function and it is a base class function and your are in the derived class so you can call the base class function from your derived class.

Take my Post #3 and add this main():
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     Derived obj.
  4.     obj.Hello();
  5. }
  6.  
Keep in mind a derived object contains an embedded base object. The derived object can call methods in its own embedded base object by using base class methods. So here the derived object is simple calling a base class method on itself.

It the Base objects that you can't create.
May 18 '08 #7
arnaudk
424 256MB
Your post #3 was indeed clear about that. Thanks again.
May 18 '08 #8

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
3 posts views Thread by Dmitry Prokoptsev | last post: by
4 posts views Thread by Eric | last post: by
7 posts views Thread by sam_cit | last post: by
10 posts views Thread by Rahul | last post: by
2 posts views Thread by manjuks | last post: by

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.