• Advertisement
Sign in to follow this  

C++: hiding operator =

This topic is 3392 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement