Like Tree2Likes
  • 2 Post By whiteflags

about destructor

This is a discussion on about destructor within the C++ Programming forums, part of the General Programming Boards category; Hi Guys, Do we have to define a destructor when making a class or the destructor is called automatically when ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    96

    about destructor

    Hi Guys,

    Do we have to define a destructor when making a class or the destructor is called automatically when we destroy any object?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    What book are you using to learn C++?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    96
    Well I was using a book but I lost it ,it was by Deitel (I don't remember the name of the book).

    But now I just use links on internet to study about C ++ language.

    I actually wanted to know that what is the use of writing a destructor in a class if it is called automatically
    and destroys the object?
    Last edited by student111; 06-22-2012 at 02:35 AM. Reason: adding more

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by student111
    Well I was using a book but I lost it ,it was by Deitel (I don't remember the name of the book).

    But now I just use links on internet to study about C ++ language.
    Ah. Well, since you lost it, you might as well get a new one. Accelerated C++ should be fine especially since you already have some C++ knowledge.

    Quote Originally Posted by student111
    I actually wanted to know that what is the use of writing a destructor in a class if it is called automatically
    and destroys the object?
    A good introductory book on C++ would explain this to you at some point.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,525
    Quote Originally Posted by student111 View Post
    Well I was using a book but I lost it
    clean your room!
    iMalc and Rodaxoleaux like this.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,262
    The compiler will always call the destructor when an objects lifetime ends.

    If you don't provide a destructor then the compiler will generate one automatically. This is the ideal situation; you just let the compiler take care of it.

    However, if your object has things that it must clean up itself, then you will need to provide a destructor to take care of those things.

    Read about "The Rule of Three (C++)"
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Jun 2012
    Location
    Bangalore
    Posts
    5
    Quote Originally Posted by student111 View Post
    Hi Guys,

    Do we have to define a destructor when making a class or the destructor is called automatically when we destroy any object?
    yes, destructor called automatically if you don't provide destructor. but the main use of manual destructor is to deallocate dynamic memory variables means pointers..!! so before the scope of object ends, destructor is executed lastly.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    531
    Quote Originally Posted by student111 View Post
    I actually wanted to know that what is the use of writing a destructor in a class if it is called automatically
    and destroys the object?
    There's a limit to what the compiler can do automatically for you. For example, it'll automatically call the destructors for any member classes of your class - it knows what members your class has at compile time.
    There's plenty the compiler can't possibly know. Most common is freeing dynamically allocated memory -- often you'll want to free what you've used. Other common examples from the net: opened files, sockets, database connections.... you have to shut down such things, as you can't possible expect the compiler to know how. I don't really like those examples -- I think the simpler the better.
    If you need stuff done whenever a class instance is destroyed, it goes in the destructor.

    If you've written your own destructor, it's still automatically called and all of the automatic stuff still happens automatically. You just add to it when you need to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hel with destructor
    By byebyebyezzz in forum C++ Programming
    Replies: 6
    Last Post: 08-03-2011, 01:16 PM
  2. Help with destructor
    By C_programmer.C in forum C++ Programming
    Replies: 8
    Last Post: 06-17-2011, 05:57 AM
  3. How to use a destructor
    By Noobie in forum C++ Programming
    Replies: 6
    Last Post: 12-02-2003, 11:40 PM
  4. destructor
    By bharathbangera in forum C++ Programming
    Replies: 5
    Last Post: 05-18-2003, 12:31 PM
  5. Destructor help
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 04:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21