defining or not defining destructors

Discussion in 'C++' started by johny smith, Jun 30, 2004.

  1. johny smith

    johny smith Guest

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

    thanks!
     
    johny smith, Jun 30, 2004
    #1
    1. Advertising

  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?


    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! =-----
     
    Chris Johnson, Jun 30, 2004
    #2
    1. Advertising

  3. 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.
     
    E. Robert Tisdale, Jun 30, 2004
    #3
  4. > 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
     
    Niels Dybdahl, Jun 30, 2004
    #4
  5. In message <40e27803$0$184$>, Niels Dybdahl
    <-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
     
    Richard Herring, Jun 30, 2004
    #5
  6. "johny smith" <> skrev i en meddelelse
    news:...
    > 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
     
    Peter Koch Larsen, Jul 1, 2004
    #6
  7. "Howard" <> wrote in message
    news:C__Ec.173365$...
    >
    > "Peter Koch Larsen" <> 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
     
    Rufus V. Smith, Jul 1, 2004
    #7
  8. johny smith

    Howard Guest

    "Peter Koch Larsen" <> 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
     
    Howard, Jul 1, 2004
    #8
  9. "Howard" <> skrev i en meddelelse
    news:C__Ec.173365$...
    >
    > "Peter Koch Larsen" <> 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!! ;-)
     
    Peter Koch Larsen, Jul 2, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul M

    Destructors...

    Paul M, Feb 5, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    437
    Alvin Bruney [MVP]
    Mar 2, 2004
  2. joe martin
    Replies:
    3
    Views:
    387
    Francis Glassborow
    Jul 16, 2003
  3. Shubhadeep

    Virtual Destructors????

    Shubhadeep, Jul 15, 2003, in forum: C++
    Replies:
    1
    Views:
    387
    Jakob Bieling
    Jul 15, 2003
  4. Rajesh Garg
    Replies:
    3
    Views:
    3,528
    Victor Bazarov
    Jul 22, 2003
  5. qazmlp
    Replies:
    7
    Views:
    504
    Howard
    Jul 27, 2004
Loading...

Share This Page