This is because D2
already exists. You have created it just line above with
Distance D1(11, 10), D2;
^
So the meaning of the =
is operator=
. The object is assigned new value ( this new value results from a call to operator() ( int, int, int)
on D1
) and not created ( constructed) with some value.
To call a copy constructor you need to assign a value to object in the line of its creation
int main() {
Distance D1(11, 10);
Distance D2( D1); // calls copy ctor
Distance D3 = D1; // calls copy ctor
return 0;
}
but
int main() {
Distance D1(11, 10);
Distance D2;
D2 = D1; // calls operator=
return 0;
}