Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > defining or not defining destructors

Reply
Thread Tools

defining or not defining destructors

 
 
johny smith
Guest
Posts: n/a
 
      06-30-2004
If I have a simple class with say a couple of integers only is there any
need for me to provide a destructor?

thanks!


 
Reply With Quote
 
 
 
 
Chris Johnson
Guest
Posts: n/a
 
      06-30-2004
johny smith <(E-Mail Removed)> wrote:
> If I have a simple class with say a couple of integers only is there any
> need for me to provide a destructor?


If the "simple class" does not have invariants; resource allocation,
exception cleanup, or the potential to be derived later on, then generally
speaking - no, you will not need to provide one.

The best answer I could offer is to make sure you understand what the
destructor is for rather than give a "general rule of thumb" guideline.
This includes but is not limited to the constructor and most importantly
the class itself.
--
Chris Johnson
~
~
:wq


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      06-30-2004
johny smith wrote:

> If I have a simple class with say a couple of integers only,
> is there any need for me to provide a destructor?


class X {
private:
// representation
int a, b;
public:
// . . .
/*
X& operator=(const X& x) {
a = x.a;
b = x.b;
return *this;
}
X(void) { }
X(const X& x): a(x.a), b(x.b) { }
~X(void) { }
*/
};

I *always* define the assignment operator,
default constructor, copy constructor and destructor
then /*comment*/ them out!

This documents the fact that I did *not* forget them
but allowed the compiler to supply them as recommended.
 
Reply With Quote
 
Niels Dybdahl
Guest
Posts: n/a
 
      06-30-2004
> If I have a simple class with say a couple of integers only is there any
> need for me to provide a destructor?


If you want to be able to derive a new class from that class and the new
class might need a destructor, then you should add a virtual destructor. A
simple destructor is not enough in that case !

Niels Dybdahl


 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      06-30-2004
In message <40e27803$0$184$(E-Mail Removed)>, Niels Dybdahl
<(E-Mail Removed)-graphics.com> writes
>> If I have a simple class with say a couple of integers only is there any
>> need for me to provide a destructor?

>
>If you want to be able to derive a new class from that class and the new
>class might need a destructor,


.... and there is a possibility of deleting instances of the derived
class through a pointer to the base class ...

> then you should add a virtual destructor. A
>simple destructor is not enough in that case !
>


--
Richard Herring
 
Reply With Quote
 
Peter Koch Larsen
Guest
Posts: n/a
 
      07-01-2004

"johny smith" <(E-Mail Removed)> skrev i en meddelelse
news:(E-Mail Removed)...
> If I have a simple class with say a couple of integers only is there any
> need for me to provide a destructor?
>
> thanks!
>
>

No need at all. In fact, even if the class contains more complex types there
is still no need to write a constructor:

class demo
{
std::string s;
std::vector v;
....
};

demo is a complex class where the two elements shown (s and v) does have
"real" destructors - destructors that have a job to do (memory management in
this case), but there is still no need to write your own constructor - the
compiler generated destructor is just fine.
My recommendation is that you code in a way so that you normally wont have
to write any destructors at all. The only exception should be for classes
that manage some resource in one way or another. This way, the default
generated copy constructor and assignment operator will also be okay and one
less source of error has been removed.

Kind regards
Peter


 
Reply With Quote
 
Rufus V. Smith
Guest
Posts: n/a
 
      07-01-2004

"Howard" <(E-Mail Removed)> wrote in message
news:C__Ec.173365$(E-Mail Removed)...
>
> "Peter Koch Larsen" <(E-Mail Removed)> wrote in message news:2DSEc.20278
>
> > ...and one less source of error has been removed.

>
> I'm scratching my head, trying to figure out how to remove one less source
> of error...?
>
> -Howard
>

LMAO!

That's another one for the DNRC newsletter!

Rufus


 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      07-01-2004

"Peter Koch Larsen" <(E-Mail Removed)> wrote in message news:2DSEc.20278

> ...and one less source of error has been removed.


I'm scratching my head, trying to figure out how to remove one less source
of error...?

-Howard




 
Reply With Quote
 
Peter Koch Larsen
Guest
Posts: n/a
 
      07-02-2004

"Howard" <(E-Mail Removed)> skrev i en meddelelse
news:C__Ec.173365$(E-Mail Removed)...
>
> "Peter Koch Larsen" <(E-Mail Removed)> wrote in message news:2DSEc.20278
>
> > ...and one less source of error has been removed.

>
> I'm scratching my head, trying to figure out how to remove one less source
> of error...?
>
> -Howard
>
>
>
>


Arghhh!!


 
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
If declared as virtual in base, its derived version also is virtual. Why not for destructors? qazmlp C++ 7 07-27-2004 03:10 PM
Destructors... Paul M ASP .Net 2 03-02-2004 06:20 AM
Can we have private constructors and destructors Rajesh Garg C++ 3 07-22-2003 10:13 PM
destructors for statically declared objects joe martin C++ 3 07-16-2003 09:30 PM
Virtual Destructors???? Shubhadeep C++ 1 07-15-2003 06:46 PM



Advertisments