Thread: about destructor

  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
    28,413
    What book are you using to learn C++?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by student111 View Post
    Well I was using a book but I lost it
    clean your room!

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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
    583
    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-03-2003, 12:40 AM
  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