why is the copy constructor called twice?

In my Date class I added a static integer that keeps track of the # of objects created, but the count is incorrect and it begins when I call my post-fix increment. The first copy constructor call is called when I declare the temp, and the second call is when I return my temp. After the return only one destructor is called. I verified this by stepping through the code in VC++.

Date Date::operator ++(int)
{
Date temp(*this);
nextDay();
return temp;
}
I fixed my code to get the correct # count of objects, but I'm still puzzled as to why the copy constructor is called twice. This was my calling function in my driver.cpp code:

cout << "\nd++ is " << d++;
It is because you are not returning 'temp' itself, you are returning a copy of 'temp', thus another copy constructor is called. If you want to not call the copy constructor there, you would want to return by reference.
Topic archived. No new replies allowed.