By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,101 Members | 996 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,101 IT Pros & Developers. It's quick & easy.

defining or not defining destructors

P: n/a
If I have a simple class with say a couple of integers only is there any
need for me to provide a destructor?

thanks!
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
johny smith <pr**************@charter.net> 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! =-----
Jul 22 '05 #2

P: n/a
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.
Jul 22 '05 #3

P: n/a
> 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
Jul 22 '05 #4

P: n/a
In message <40*********************@dtext02.news.tele.dk>, Niels Dybdahl
<nd*@fjern.detteesko-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
Jul 22 '05 #5

P: n/a

"johny smith" <pr**************@charter.net> skrev i en meddelelse
news:10*************@corp.supernews.com...
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
Jul 22 '05 #6

P: n/a

"Peter Koch Larsen" <pk*****@mailme.dk> 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


Jul 22 '05 #7

P: n/a

"Howard" <al*****@hotmail.com> wrote in message
news:C_*********************@bgtnsc04-news.ops.worldnet.att.net...

"Peter Koch Larsen" <pk*****@mailme.dk> 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
Jul 22 '05 #8

P: n/a

"Howard" <al*****@hotmail.com> skrev i en meddelelse
news:C_*********************@bgtnsc04-news.ops.worldnet.att.net...

"Peter Koch Larsen" <pk*****@mailme.dk> 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!! ;-)
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.