class C
{
public:
C(C& c)
{
i = c.i;
j = 100;
}
C() : i(0), j(0)
{
}
int i, j;
};
C func(C c)
{
return c;
}
int main()
{
C c;
c = func(c)
// What is the value of j?
}
Above is a class with an unusual copy constructor. Instead of copying i and j, it copies i and assigns something else to j. What happens when I pass an object of the class to the function?
Edit: It just seems like such a tricky thing to do in a program…
The copy constructor may be called, in which case your weird behavior happens.
Or, the compiler may elide the copy (this is specifically allowed by the standard), breaking your expectations. In this particular case that isn’t allowed, but in many contexts it is.
So don’t write copy-constructors that do weird things. (Or else we will call you
auto_ptr)