C++: hiding operator =
Example code (I have declared operator= as a private method without implementing it):
class Foo
{
public:
private:
Foo & operator= (const Foo & r);
};
int main()
{
// ** Case 1: **
Foo a;
// Works
Foo b = a;
// ** Case 2: **
Foo c;
Foo d;
// Linkage error as expected
c = d;
return 0;
}
My question is: why does the first substitution case work and compile? What happens there?
(I'm using g++ 4.2.4)
Have a look at the "Rule of three":
http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)
-> http://en.wikipedia.org/wiki/Copy_constructor
-> http://en.wikipedia.org/wiki/Assignment_operator_in_C%2B%2B
http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)
-> http://en.wikipedia.org/wiki/Copy_constructor
-> http://en.wikipedia.org/wiki/Assignment_operator_in_C%2B%2B
Quote:Original post by FinnCoderIn the first case, the compiler calls the copy constructor, not operator=, since the object is being assigned and constructed at the same time.
Example code (I have declared operator= as a private method without implementing it):
class Foo
{
public:
private:
Foo & operator= (const Foo & r);
};
int main()
{
// ** Case 1: **
Foo a;
// Works
Foo b = a;
// ** Case 2: **
Foo c;
Foo d;
// Linkage error as expected
c = d;
return 0;
}
My question is: why does the first substitution case work and compile? What happens there?
(I'm using g++ 4.2.4)
In the second case, the object is default constructed, then has operator= called on it.
So, if you want to prevent the first case, make your copy constructor private too.
What phresnel said.
The latter example which fails is assigning one default constructed object to another. So it goes:
-construct c
-construct d
-assign d to c
The former which works probably replaces a default construction with a copy construction:
-default construct a
-copy construct b
Turn off all optimisations and it might well fail as expected. Hence the rule-of-three...
The latter example which fails is assigning one default constructed object to another. So it goes:
-construct c
-construct d
-assign d to c
The former which works probably replaces a default construction with a copy construction:
-default construct a
-copy construct b
Turn off all optimisations and it might well fail as expected. Hence the rule-of-three...
Quote:Original post by Evil Steve
In the first case, the compiler calls the copy constructor, not operator=, since the object is being assigned and constructed at the same time.
In the second case, the object is default constructed, then has operator= called on it.
So, if you want to prevent the first case, make your copy constructor private too.
Yeah, you're right. I should have figured that out :) Thanks to all for your replies!
Quote:Original post by zdlr
The former which works probably replaces a default construction with a copy construction... Turn off all optimisations and it might well fail as expected. Hence the rule-of-three...
It's not an optimization issue; 'Foo a = b' is necessarily an initialization syntax. This is presumably motivated by backwards compatibility (since in C you couldn't write things like 'int x(42);').
Quote:Original post by Zahlman
you couldn't write things like 'int x(42);'
Soon you can write int x{42}; in C++0x. Yes, those are curly braces :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement