destruct a set of classes

hello,

i have a question a about destructor.

if i have a set of classes
1
2
#include <set>
                                 set <classe_name> exp;

should i desctruct these classes before returning a value in my main function.
Last edited on
No. If the set contains objects, the copies in the set will be correctly destroyed.

If the set contains pointers to objects, the pointers won't be followed, and if you need them destroyed at that point, you'll need to do it yourself.
closed account (S6k9GNh0)
I don't think it's needed. Say for instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
// ifndef BOB_H
// define BOB_H
class Bob
{
  public:
    Bob(char * name);
    Bob();
    ~Bob();
    int getName();
  private:
    char * Name; //???
}
// ENDIF //BOB_H 


Here we have the constructors, and a simple GetName function declaration that fetches the private string, Name. When this is deconstructed, everything is put out of scope and unallocated. That's the point of it anyways. If you have variables that don't automatically go out of scope, then the deconstructor needs to take care of this other wise you will have very large memory leaks.

http://www.tech-recipes.com/rx/1231/c-destructor/

Good tutorial on why and when. Anyways, the point I made the class is to demonstrate when a deconstructor isn't really needed. All of the variables will go out of scope. As a result, there is no point in the deconstructor. The variables are called to NULL and are no longer in existance therefor they have gone out of scope and are not accessible any longer.

^ from what I've learned. I may be wrong on some of this.
Last edited on
Topic archived. No new replies allowed.