// Pass and return BY VALUE:
HowMany f(HowMany x) {
x.print("x argument inside f()");
return x;
}
int main() {
HowMany h;
HowMany::print("after construction of h");
HowMany h2 = f(h);
HowMany::print("after call to f()");
} ///:~
Output:
after construction of h: objectCount = 1
x argument inside f(): objectCount = 1
~HowMany(): objectCount = 0
after call to f(): objectCount = 0
~HowMany(): objectCount = -1
~HowMany(): objectCount = -2
Will any one tell me the reason behind the output not coming as expected.What exactly is this "bitcopy"?
Any link with relevant info will do.
Thanx in advance
Will any one tell me the reason behind the output not coming as expected.
Because you have not provided your own copy constructor - the default
compiler provided copy constructor isn't going to update the objectCount variable.
So when the compiler creates the parameter to pass to the f function and to create the return object from from the f function - it uses the copy constructor, which does not update the objectCount variable, but your destructors decreses the objectCount variable when these objects get destructed - so there is a mistatch in the record keeping between objects created and objects deleted destructed.