Destructor is called after copy

Whenever my copy constructor is called, my destructor destroys is immediately after.


My copy constructor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
CS1CStudent::CS1CStudent(const CS1CStudent& otherCS1CStudent)
{
	cout << "\n***************************\n";
        cout << "**Copy Constructor Called**\n";
        cout << "***************************";
          
        //copy name
        changeName(otherCS1CStudent.getName());
        
        //copy id
        changeId(otherCS1CStudent.getId());

        //copy phone #
        changePhone(otherCS1CStudent.getPhone());

        // copy age
        changeAge(otherCS1CStudent.getAge());

        //copy gender
        changeGender(otherCS1CStudent.getGender());
 
        //copy class standing
        changeClass(otherCS1CStudent.getClass());

        //copy gpa
        changeGpa(otherCS1CStudent.getGpa());
   
        //copy score
        totalScore = otherCS1CStudent.totalScore;

        //copy  knowledge of java
        java = otherCS1CStudent.java;

        //copy graduation date
        grad = otherCS1CStudent.grad;

        //copy street name
        street = new char [strlen(otherCS1CStudent.street) +1];
        strcpy(street, otherCS1CStudent.street);

        //copy city name
        city = new char[strlen(otherCS1CStudent.city) +1];
        strcpy(city, otherCS1CStudent.city);

        //copy state name
        state = new char[strlen(otherCS1CStudent.state) +1];
        strcpy(state, otherCS1CStudent.state);

        //copy zip
        zip = otherCS1CStudent.zip;

}


My destructor
1
2
3
4
5
6
7
8
9
CS1CStudent::~CS1CStudent()
{
      cout << "\n*********************\n";
      cout << "**Destructor Called**\n";
      cout << "*********************";
      delete [ ] street;
      delete [ ] city;
      delete [ ] state;
}



In my main I have
1
2
3
4
5
6
 CS1CStudent* cs1cPtr[4];

        cs1cPtr[0] = new CS1CStudent;
        cs1cPtr[1] = new CS1CStudent;
        cs1cPtr[2] = new CS1CStudent;
        cs1cPtr[3] = new CS1CStudent;


and this is where the problem occurs
*cs1cPtr[2] = cs1cPtr[0]->passAndReturnByCopy(*cs1cPtr[0]);

the function passAndReturnByCopy:
1
2
3
4
CS1CStudent CS1CStudent::passAndReturnByCopy(CS1CStudent cs1c)
{
	return cs1c;
}


and finally, the private members
1
2
3
4
5
6
7
8
9
private:
        int totalScore;
        bool java;
        dateType grad;

        char* street;
        char* city;
        char* state;
        int zip;



The copy constructor is called twice, once when you pass an object by value, and once when the object is returned from a function by value. But why is the destructor being called twice?
The copy constructor is called twice, once when you pass an object by value, and once when the object is returned from a function by value. But why is the destructor being called twice?


Two objects were created. Why do you think they won't both be destroyed?
Topic archived. No new replies allowed.