473,322 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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!
Jul 22 '05 #1
8 1405
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
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
> 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
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

"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

"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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
3
by: Nuno Barros | last post by:
Cn someone tell me if when i call the destructor of a derivated class, the destructor of the base class is called implicitly? Or shall i call the destructor by myself? Thanks in advance ...
12
by: Ross Boylan | last post by:
I am trying to understand under what circumstances destructors get called with std::vector. I have an application in which I will put real objects, not just pointers, in the vector. 1. The...
26
by: Michi Henning | last post by:
I've been having problem with destructors in the context of having ported C# code developed under .NET to Mono. What happens is that, on a dual-CPU machine, various parts of the code crash randomly...
8
by: Edward Diener | last post by:
I have a __value class which uses some legacy C++ code. So I wrapped the legacy C++ code in another __nogc class and have a pointer to that class as a member of my __value class. When the __value...
3
by: alex.gman | last post by:
If I have code like this int f() { // ... stuff ... g(); if(x > 0) return (x+4); // ... more stuff ... always_call(z); return y; }
5
by: PaperPilot | last post by:
I have defined a vector and its iterator as such: typedef std::vector< WindsTableElement > WindTable; WindTable wt_; WindTable::iterator windIterator_; I get no errors during compile, but...
6
by: mlw | last post by:
Could someone explain why there is no destructor in Java classes? There are many times you need to be called WHEN an object goes out of scope and not when it will eventally be freed.
6
by: Jeff Newman | last post by:
Hello, Could anyone explain to me why the following class's destructor shows up as having multiple branches? (At least as judged by gcov 4.1.2 when compiled with gcc 4.1.2 ): struct blah {...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.