string destructor

This is a discussion on string destructor within the C++ Programming forums, part of the General Programming Boards category; In a class i have many string (#include <string>) variables. In the destructor do i have to do anything special ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    36

    string destructor

    In a class i have many string (#include <string>) variables. In the destructor do i have to do anything special for the string? i tried delete name_of_string and the compiler complains that delete wants pointer.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    the string class manages its memory for you. You don't have to delete memory for string objects in the destructor.
    You're only born perfect.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >In the destructor do i have to do anything special for the string?
    No

    >i tried delete name_of_string and the compiler complains that delete wants pointer.
    Only use delete when you use new. For every new, you need a delete.

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    That's one of the wonderful thing about such classes in the STL. Internally, all memory of a std::string is managed for you. That's why you can do a std::string s = "blah"; without calling new. A rule of thumb to keep in mind is that you should call delete exactly once for every call to new, and since you never call new for a string the way you're using it, you should never try to delete it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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