| View previous topic :: View next topic |
| Author |
Message |
johny smith Guest
|
Posted: Wed Jun 30, 2004 2:17 am Post subject: defining or not defining destructors |
|
|
If I have a simple class with say a couple of integers only is there any
need for me to provide a destructor?
thanks!
|
|
| Back to top |
|
 |
Chris Johnson Guest
|
Posted: Wed Jun 30, 2004 3:34 am Post subject: Re: defining or not defining destructors |
|
|
johny smith <princetonharvard (AT) charter (DOT) net> wrote:
| Quote: | 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! =-----
|
|
| Back to top |
|
 |
E. Robert Tisdale Guest
|
Posted: Wed Jun 30, 2004 4:17 am Post subject: Re: defining or not defining destructors |
|
|
johny smith wrote:
| Quote: | 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.
|
|
| Back to top |
|
 |
Niels Dybdahl Guest
|
Posted: Wed Jun 30, 2004 8:21 am Post subject: Re: defining or not defining destructors |
|
|
| Quote: | 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
|
|
| Back to top |
|
 |
Richard Herring Guest
|
Posted: Wed Jun 30, 2004 11:04 am Post subject: Re: defining or not defining destructors |
|
|
In message <40e27803$0$184$edfadb0f (AT) dtext02 (DOT) news.tele.dk>, Niels Dybdahl
<ndy (AT) fjern (DOT) detteesko-graphics.com> writes
| Quote: | 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 ...
| Quote: | then you should add a virtual destructor. A
simple destructor is not enough in that case !
|
--
Richard Herring
|
|
| Back to top |
|
 |
Peter Koch Larsen Guest
|
Posted: Thu Jul 01, 2004 11:32 am Post subject: Re: defining or not defining destructors |
|
|
"johny smith" <princetonharvard (AT) charter (DOT) net> skrev i en meddelelse
news:10e48mf5p4mb664 (AT) corp (DOT) supernews.com...
| Quote: | 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
|
|
| Back to top |
|
 |
Prateek R Karandikar Guest
|
Posted: Thu Jul 01, 2004 8:59 pm Post subject: Re: defining or not defining destructors |
|
|
johny smith wrote:
| Quote: | If I have a simple class with say a couple of integers only is there
any
need for me to provide a destructor?
thanks!
|
If you don't write a dtor, the compiler will provide its own. If you
are happy with the default, no need to write your own.
-- --
Abstraction is selective ignorance.
-Andrew Koenig
-- --
|
|
| Back to top |
|
 |
Rufus V. Smith Guest
|
Posted: Thu Jul 01, 2004 9:00 pm Post subject: Re: defining or not defining destructors |
|
|
"Howard" <alicebt (AT) hotmail (DOT) com> wrote
| Quote: |
"Peter Koch Larsen" <pklspam (AT) mailme (DOT) dk> wrote
...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
|
|
| Back to top |
|
 |
Howard Guest
|
Posted: Thu Jul 01, 2004 9:03 pm Post subject: Re: defining or not defining destructors |
|
|
"Peter Koch Larsen" <pklspam (AT) mailme (DOT) dk> wrote
| Quote: | ...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
|
|
| Back to top |
|
 |
Peter Koch Larsen Guest
|
Posted: Fri Jul 02, 2004 8:51 am Post subject: Re: defining or not defining destructors |
|
|
"Howard" <alicebt (AT) hotmail (DOT) com> skrev i en meddelelse
news:C__Ec.173365$Gx4.173318 (AT) bgtnsc04-news (DOT) ops.worldnet.att.net...
| Quote: |
"Peter Koch Larsen" <pklspam (AT) mailme (DOT) dk> wrote
...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!! ;-)
|
|
| Back to top |
|
 |
|