Sign in to follow this  
FinnCoder

C++: hiding operator =

Recommended Posts

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)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Quote:
Original post by FinnCoder
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 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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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!


Share this post


Link to post
Share on other sites
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);').

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this