defining or not defining destructors

J

johny smith

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

thanks!
 
C

Chris Johnson

johny smith said:
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.
 
E

E. Robert Tisdale

johny said:
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.
 
N

Niels Dybdahl

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
 
R

Richard Herring

Niels Dybdahl said:
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 ...
 
P

Peter Koch Larsen

johny smith said:
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
 
R

Rufus V. Smith

Howard said:
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
 
H

Howard

Peter Koch Larsen said:
...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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top