Incorrect de-allocation of object

Hi All,

I am facing a problem in following code:
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
class CVector 
{public:
    int x;
    int y;
    static int count;
    CVector(){count++;}

    CVector(int a,int b)
    {
        x=a;
        y=b;
    }
    int thisOp(CVector&);
    CVector operator +(CVector);
    ~CVector(){count--;}
};

int CVector::count=0;
int CVector:: thisOp(CVector & a)
{
if (this==&a)
    return true;
else return false;
}
CVector CVector::operator +(CVector param)
{
    CVector temp;
    temp.x=this->x+param.x;
    temp.y=this->y+param.y;
    cout<<"Count"<<CVector::count<<endl;
    return temp;
}

int main()
{
    CVector a(1,2), b(2,3),c;
    cout<<"Count"<<CVector::count<<endl;
    c=a+b;
    //cout<<"VAlue in c";
    //cout<<endl<<c.x<<"\t"<<c.y<<endl;
return 0;
}



The execution goes like this:

1. Object 'c' is created using default constr. So, count=1.
2. operator+() is called.
3. Object 'temp' is created which is local to operator+(). So, count=2
4. operator+() completely executed. 'temp' is destroyed as it is local to that function. So, count=1.
5. The weird thing is: Destructor is called one more time. And count=0.

I can't understand why object 'c' is getting destroyed on completing operator+() execution?

And if 'c' is not getting destroyed, why the destructor is called twice on complete execution of operator+().

Please advice.

Should be
 
const CVector CVector::operator+(const CVector& param);

I think it's destructor of temporary created object in operator+. Because you pass variable by value. It creates temporary object by copy constructor and then this object is destroying
Thanks Denis.

It worked. But I have one more Question.

I agree that since I passed the object by value, it would destroy that object. (param)

But when i pass the object by value, different memory will be allocated to it and the default constr should be called. And in case default constr is called, the value of count should also increase.
You are not exactly right, in this case will be called copy constructor(not default as you said)
Thanks a lot Denis.

Your reply clarified a lot of concepts.

Looking forward to ask more queries.
To check if Copy constr is called, i defined one copy constr as follow:

1
2
3
4
5
6
7
8
    CVector(CVector & ob)
    {
        x=ob.x;
        y=ob.y;
        count++;

    }


Now, the compiler is giving an error in main method:
 
 c=a+b;


The error is as follows:

ClassesImplement.cpp:52: error: no matching function for call to `CVector::CVector(CVector)'


To resolve, I changed the declaration of c as:
 
CVector c=a+b;


But it is still giving the same error.


CVector( const CVector& ob )

is the right declaration.
Thanks jsmith.

Can you please explain the reason for putting "const" when passing by reference.

This is a compile-time check that verifies that you are not modifying the object in your function.

More on const-correctness here.
http://www.parashift.com/c++-faq-lite/const-correctness.html
Topic archived. No new replies allowed.