Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > virtual destructor

Reply
Thread Tools

virtual destructor

 
 
JKop
Guest
Posts: n/a
 
      05-23-2004
JKop posted:

> Mammal restt = new Dog;



TYPO TYPO TYPO


Mammal* restt = new Dog;


-JKop
 
Reply With Quote
 
 
 
 
Jorge Rivera
Guest
Posts: n/a
 
      05-23-2004
> A pure virtual destructor is sometimes used to make an abstract class,
> when there are no other virtual functions in that base class than make
> sense to be pure. You also still provide the destructor implementation
> in that case.
>


I have tried not providing an implementation to the destructor, and I
always get link errors.

Ex:

class PureVirtual
{
public:
virtual ~PureVirtual()=0;
virtual implementMe()=0;
};

class Derived : public PureVirtual
{
public:
~Derived(){}
virtual implementMe(){}
};

int main()
{
Derived a;
return 0;
}


This will start whining about PureVirtual::~PureVirtual() not found...
 
Reply With Quote
 
 
 
 
Jeff Schwab
Guest
Posts: n/a
 
      05-23-2004
JKop wrote:
> Thanks Stephen Waits and Duane Hebert for your replies, although I found the
> both of them to be HEAVILY contrived.
>
>
> Why would one function delete memory allocated by another?


For a number of very good reasons. For example, memory is allocated in
an object's constructor, then deleted in the object's destructor.

> And why would you do the following:
>
>
> Mammal restt = new Dog;
>
> delete restt;


You wouldn't. However, you might do something like this:

std::vector< Animal* > animals;

// Populate the vector according to factors
// available only at run-time.

// Call on each animal to do something (e.g.
// make noise), and rely on polymorphism to
// make sure the dogs bark, while the cats
// meow.

// Delete each animal in the vector through
// the pointer-to-base.

> Looks like child's play to me.




> That said, the overhead of declaring the destructor virtual is minimal by
> today's standards, so maybe your point is stronger than mine.


Are you sure about that? Not all code is meant for desktop machines
with plentiful RAM. Anyway, I find the "hardware is cheap, so let's
waste it" philosophy distasteful at best.
 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      05-23-2004
David Harmon wrote:
> On Sat, 22 May 2004 12:29:57 -0400 in comp.lang.c++, Jeff Schwab
> <(E-Mail Removed)> wrote,
>
>>>Can a destructor as "pure virtual"?

>>
>>Yes, but I can't think of a case when it's a good idea.

>
>
> A pure virtual destructor is sometimes used to make an abstract class,
> when there are no other virtual functions in that base class than make
> sense to be pure. You also still provide the destructor implementation
> in that case.


If you provide an implementation for the destructor, in what way is the
destructor "pure virtual?" If the destructor of the base class is pure
virtual, how do you instantiate the derived classes? Shouldn't this
code fail to link properly?

struct Base { virtual ~Base ( ) = 0; };
struct Derived: Base { ~Derived ( ) { } };

int main ( ) { Derived d; }
 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      05-23-2004
Denis Remezov wrote:

> 10.3.8 says:
> "A virtual function [...] shall be defined, or declared pure [...] or both;
> [...]"
>
> So yes, you can provide a definition for a pure virtual function.


Thanks, Denis. I've never seen a pure virtual function with a
definition before now. Live and learn!
 
Reply With Quote
 
Stephen Waits
Guest
Posts: n/a
 
      05-24-2004
JKop wrote:
> Thanks Stephen Waits and Duane Hebert for your replies, although I found the
> both of them to be HEAVILY contrived.
>
> Why would one function delete memory allocated by another?


MyContainer< Base > blah;

blah.push_back( new Derived );

Now stop fighting it and embrace it.. it is how it is.

--Steve
 
Reply With Quote
 
Luther Baker
Guest
Posts: n/a
 
      05-24-2004
Stephen Waits wrote:
> JKop wrote:
>
>> Thanks Stephen Waits and Duane Hebert for your replies, although I
>> found the both of them to be HEAVILY contrived.
>>
>> Why would one function delete memory allocated by another?

>
>
> MyContainer< Base > blah;
>
> blah.push_back( new Derived );
>
> Now stop fighting it and embrace it.. it is how it is.



Fix the typo

<Base*>

and beware, your example is leaking memory.

Hth,

-Luther
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      05-24-2004
JKop <(E-Mail Removed)> wrote:
> And why would you do the following:
>
> Mammal restt = new Dog;
> delete restt;
>
> Looks like child's play to me.


Looks like polymorphism to me (go look it up in your C++ books).
The idea is that you could write a function, eg. "greet",
and call it:
restt->greet();
that works for any Mammal, but calls different code for different
mammals. (eg. Dog::greet() { bark(); } and Fish::greet() { swim(); }
This sort of thing happens a lot in real world programming. We'd
appreciate it if you stop your habit of dismissing things that you
don't see a use for.
 
Reply With Quote
 
Luther Baker
Guest
Posts: n/a
 
      05-24-2004
JKop wrote:
>
> And why would you do the following:
>
>
> Mammal restt = new Dog;
>
> delete restt;
>


I'm not sure I understand your question - so I apologize if I'm off
base, but ... programming to interfaces is a fundamental principal of OO
design. One way to do this is to use base class pointers to concrete
derived class implementations.

To get a base class pointer to call a derived class method, the method
must be declared virtual. Lets look at a base class Theme:

Theme* theme = ThemeFactory.getUserTheme();

In this case, the factory will return a concrete instance of a class
derived from theme. Any call to a virtual method made by *theme* will
call the derived class' method. Any call to a non-virtual method made by
*theme* will enact its own implementation of said method.

Now, if the Derived Class constructor creates resources. For example,
the AMEX theme creates a pool of database connections to the Dow Jones
interface portal available on the local network. So, unbeknownst to the
base class, the derived class has allocated resources.

The resources could be heap memory for char*s or it might be something
more elaborate. Whatever the case - when the base class pointer is
eventually deleted, how are the resources the derived class acquired
going to be released?

If the base class destructor is NOT virtual, then the derived class will
never be told that the base class pointer has been deleted. If the
destructor IS virtual, then the derived class destructor will be called.

Some smaller projects may in fact get by without using virtual
destructors, but in most large projects - it would be the norm that
classes should indeed have virtual destructors.

For a better example and discussion on the topic, see Exceptional C++,
Herb Sutter, pg 75, and work through item 21. Specifically, take a look
at the guideline on page 77.

-Luther

 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      05-24-2004

Here's what I'm getting at:


Why would you cast a Dog* to a Mammal*? as in:


Mammal restt = new Dog;
delete restt;


as opposed to:


Dog restt = new Dog;
delete restt;



Anyway, I see what yous are getting at, end of discussion.


For the next version of C++ I suggest:

class Mammal
{
virtual_ifderived ~Mammal(void);
};



-JKop

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
compiler generated destructor vs class implemented destructor arun C++ 2 06-13-2006 05:43 AM
Can abstract base class have V-table?, Will the pointer to virtual destructor be entered into the virtual table? sojin C++ 12 04-07-2006 09:02 AM
Explicit destructor calls from inside base class destructor frs C++ 20 09-21-2005 09:22 AM
Virtual destructor for virtual base class? Chunhui Han C++ 2 06-24-2004 10:13 AM
virtual destructor Vs virtual method Calvin Lai C++ 7 12-18-2003 03:11 AM



Advertisments