11 Replies - 7190 Views - Last Post: 16 August 2011 - 08:39 AM Rate Topic: -----

#1 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

implementing a pure virtual destructor - why ?

Posted 15 August 2011 - 03:03 PM

Hi,

I understand the use of virtual destructor when using Polymorphism and Inheritance (to have
no memory leaks and verify we have indeed deleted the entire hierarchy), what I
don't understand is why would we want to implement a pure virtual destructor ?

Regards,
Ron

This post has been edited by passer_by: 15 August 2011 - 03:05 PM

Is This A Good Question/Topic? 0
  • +

Replies To: implementing a pure virtual destructor - why ?

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: implementing a pure virtual destructor - why ?

Posted 15 August 2011 - 03:45 PM

Wouldn't it force any inherited classes to implement a virtual destructor, thereby ensuring proper cleanup of all child classes?

This post has been edited by JackOfAllTrades: 16 August 2011 - 03:40 AM
Reason for edit:: Meant destructor, not constructor.

Was This Post Helpful? 0
  • +
  • -

#3 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

Re: implementing a pure virtual destructor - why ?

Posted 15 August 2011 - 06:08 PM

View PostJackOfAllTrades, on 15 August 2011 - 03:45 PM, said:

Wouldn't it force any inherited classes to implement a virtual constructor, thereby ensuring proper cleanup of all child classes?

a virtual constructor ?
c++ doesn't allow virtual constructors as far as I know...
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: implementing a pure virtual destructor - why ?

Posted 15 August 2011 - 09:24 PM

Well it turns out that there is a very good reason for virtual deconstructors if you plan on using polymophism!


So if you have a class say FooA and it has a non-virtual deconstructor ~FooA() and some yahoo goes and derives FooB from your class and does this:

FooA* instance = new FooB();
...
delete instance;


then the delete will call the deconstructor ~FooA and NOT the one from ~FooB unless you made your deconstructor virtual.

see here and here which were the two top links on the Google search for "virtual deconstructors"

View Postpasser_by, on 15 August 2011 - 09:08 PM, said:

View PostJackOfAllTrades, on 15 August 2011 - 03:45 PM, said:

Wouldn't it force any inherited classes to implement a virtual constructor, thereby ensuring proper cleanup of all child classes?

a virtual constructor ?
c++ doesn't allow virtual constructors as far as I know...


true but there are factory patterns that mimic the concept of a virtual constructor. i.e. see the very next topic in the C++FAQ link in my post above.
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: implementing a pure virtual destructor - why ?

Posted 15 August 2011 - 09:29 PM

ooo I didn't understand the question detail. "why pure virtual deconstructor" -- to that end I point to the second article from "The old new thing" (great blog to browse by the way).

Even if you do not specify a deconstructor the compiler creates one for you. So there really is no such thing as a class without one. So the same problem that exists with explicit deconstructors exists with implicit ones. i.e. in the example above even if I didn't make a ~FooA the compiler will and that is the deconstructor that will be called when delete instance happens.

However if I declare the deconstructor to be virtual even if I don't implement one in either class (the compiler will) the correct deconstructor will get called.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: implementing a pure virtual destructor - why ?

Posted 16 August 2011 - 03:40 AM

View Postpasser_by, on 15 August 2011 - 09:08 PM, said:

a virtual constructor ?
c++ doesn't allow virtual constructors as far as I know...


I mis-typed destructor as constructor. I will fix that. My answer was really sort of a guess. Never encountered a pure virtual destructor in the wild.
Was This Post Helpful? 0
  • +
  • -

#7 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: implementing a pure virtual destructor - why ?

Posted 16 August 2011 - 03:47 AM

Virtual destructors, according to Visual C++, require all derived classes to implement a base destructor. For example:
class Foo
{
	public:
		virtual ~Foo () = 0;
};
class FooChild: public Foo
{
	public:
		~FooChild () { }
};
This gets a linker error (undefined symbol Foo::~Foo referenced in FooChild::~FooChild).
I have yet to work out how to get around this, and the purpose of a pure virtual destructor.
Was This Post Helpful? 1
  • +
  • -

#8 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

Re: implementing a pure virtual destructor - why ?

Posted 16 August 2011 - 03:56 AM

View PostPlasticineGuy, on 16 August 2011 - 03:47 AM, said:

Virtual destructors, according to Visual C++, require all derived classes to implement a base destructor. For example:
class Foo
{
	public:
		virtual ~Foo () = 0;
};
class FooChild: public Foo
{
	public:
		~FooChild () { }
};
This gets a linker error (undefined symbol Foo::~Foo referenced in FooChild::~FooChild).
I have yet to work out how to get around this, and the purpose of a pure virtual destructor.

I'm okay with that (i.e that all the derived classes would implement a base destructor) but I yet to understand
why would I want to make the destructor pure virtual and implement it at the same time . The only reason that I
can think about is for having a common interface for a bunch of classes (that would inherit from Base) , would
use the entire methods that base has , but still the OP doesn't want to have objects of the base class , and only of the derived classes (meaning , the OP wants all the methods and found no method to act as a pure virtual - in order to make the class , abstract) .

This post has been edited by passer_by: 16 August 2011 - 03:57 AM

Was This Post Helpful? 0
  • +
  • -

#9 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: implementing a pure virtual destructor - why ?

Posted 16 August 2011 - 03:58 AM

JackOfAllTrades mentioned that he has never seen it in the wild, and I would take this and the inherent pointlessness of the concept as a fair indication that pure virtual destructors are either pointless or extremely rare.

Can you explain where you read about or encountered the issue?

P.S. The OP is you (it means "original poster").
Was This Post Helpful? 0
  • +
  • -

#10 passer_by  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 234
  • Joined: 06-March 11

Re: implementing a pure virtual destructor - why ?

Posted 16 August 2011 - 04:08 AM

View PostPlasticineGuy, on 16 August 2011 - 03:58 AM, said:

JackOfAllTrades mentioned that he has never seen it in the wild, and I would take this and the inherent pointlessness of the concept as a fair indication that pure virtual destructors are either pointless or extremely rare.

Can you explain where you read about or encountered the issue?

P.S. The OP is you (it means "original poster").

My lecturer had mentioned it one time but never gave a full explanation ..
And I meant OOP (object oriented programmer) , sorry :) , and see here

ron
Was This Post Helpful? 0
  • +
  • -

#11 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: implementing a pure virtual destructor - why ?

Posted 16 August 2011 - 04:12 AM

You shouldn't add a pure virtual function just to make the class unable to be created. First, it adds overhead which is not needed (virtual functions are relatively slow!). Second, if your class doesn't require a pure virtual function, then it is a complete class.

If it's not a complete class AND doesn't have a pure virtual function (it happens), just mark clearly in comments that it's incomplete. Any user that then proceeds to make an instance of it deserves to have their program crash.
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,421
  • Joined: 23-August 08

Re: implementing a pure virtual destructor - why ?

Posted 16 August 2011 - 08:39 AM

Maybe this link is helpful.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1