Should destructor be called ?

HI!
HI!
I coded a class called Person :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef PERSON_H_INCLUDED
#define PERSON_H_INCLUDED

#include <string>

using std :: string ;

class Person
{
    string * Name ;
public :
    Person () {} ;
    Person (string input) {Name = new string ; * Name = input ;} ;
    ~Person () {delete Name ;} ;
    void SetName (string input) {Name = new string ; * Name = input ;} ;
    string GetName () {return * Name ;} ;
};

#endif // PERSON_H_INCLUDED 


As you see there is a destructor .
And this is main .cpp :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
//#include <string>

using std :: cout ;
//using std :: string ;

#include "Person.h"

int main ()
{
    Person tmp1 ("ALI") ;
    //tmp1.SetName ("ALI") ;
    cout << tmp1.GetName () ;
    tmp1.~Person () ;
}


Is the last line (line 14) correct. should I do that?
My program crash when I do that.
How should I tell the compiler to call the constructor?

THNX IN ADVANCED!
Last edited on
The destructor is automatically called when the object gets out of scope, you don't need to call it explicitly. Try putting std::cout << "Destructor!\n" in the destructor to see how it works. (include <iostream> for this to work).

return 0; from main is good practice.

Edit; I think you're leaking memory in the SetName method. This is because you may already have newed the string pointer. When you new once again, the old pointer is no longer available whilst the string object resides in memory still.

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

#ifndef PERSON_H_INCLUDED
#define PERSON_H_INCLUDED

#include <string>

using std::string ;

class Person
{
    string *Name ;
public :
    Person () : Name(nullptr) {} ;
    Person (string &input) : Name(new string(input)) {}
    Person (const char *input) : Name(new string(input)) {}
    ~Person () {delete Name;}
    void SetName (string &input) {if (Name) delete Name; Name = new string(input);}
    string &GetName () {return *Name ;}
};

#endif // PERSON_H_INCLUDED  
Last edited on
I would suggest putting the using part in the actual functions that need it not in the global space. eg
1
2
3
4
5
6
7
8
9
10
int main( void )
{
     using std::cout;
     cout << "Hello world" << std::endl;
};
//or even this
int main( void )
{
     std::cout << "Hello world" << std::endl;
}

And as far as your clas doing
1
2
3
4
int main( void )
{
//stuff
};


And as farm as calling the destructor yes that is correct and a way to check it is if you go into the destructor function and put something like std::cout << "Destroyed" << std::endl;

and anyways you are calling the destructor twice. When the main function ends it will destroy the object tmp1 unless you make that a pointer eg
1
2
Person *tmp1;
tmp1 = new Person("ALI");

Then you would have to delete that manually which would also cause the destructor. So you could call the destructor then delete the pointer I guess

EDIT:
Bourgond beat me to most of the stuff
Last edited on
Thankl you so much Bourgond Aries and giblit.
You helped so much
And
I got what I was lookin for!
But I can't understand Bourgond's code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef PERSON_H_INCLUDED
#define PERSON_H_INCLUDED

#include <string>

using std::string ;

class Person
{
    string *Name ;
public :
    Person () : Name(nullptr) {} ;
    Person (string &input) : Name(new string(input)) {}
    Person (const char *input) : Name(new string(input)) {}
    ~Person () {delete Name;}
    void SetName (string &input) {if (Name) delete Name; Name = new string(input);}
    string &GetName () {return *Name ;}
};

#endif // PERSON_H_INCLUDED  


What does this do? Person () : Name(nullptr) {} ;
What does the colon (this sign : ) do after functions?
Is this another method of declaring functions?
What about this piece of code : Name(new string(input))
Is there a new method of calling new instead of doing this : Name = new string ; * Name = input ;

Looks like there are lots of Technics. The only thing I did was reading the tutorial on this site and some of reference part. What should I do to learn these thecnics? THNX!
The use of a colon after a constructor indicates initialization. Whilst the body of a constructor is used for assignment. One can interpret it like this:
1
2
// Initialization:
int a = 123;

1
2
3
// Assignment
int a;
a = 123;

I think it's good practice to initialize as much as you can because later you need to maybe initialize references and const variables, and they can not be assigned.

Ardeshir81 wrote:
What about this piece of code : Name(new string(input))

I believe that is the initialization of the Name pointer with a new string object containing the input that you sent.

Ardeshir81 wrote:
Is there a new method of calling new instead of doing this : Name = new string ; * Name = input;

I think they are equivalent, but I think that repetitive calls to new without deleting the object will cause a memory leak. new string(/*constructor arguments*/); is like creating an instance of your class with new: new Person; uses the empty (default) constructor. new Person("Someone"); uses the const char * constructor.

Ardeshir81 wrote:
Looks like there are lots of Technics. The only thing I did was reading the tutorial on this site and some of reference part. What should I do to learn these thecnics? THNX!


Google absolutely everything! You'll get redirected to forums (StackOverflow especially) where there are a tonne of good answers.
Topic archived. No new replies allowed.