Aug 4, 2011 at 9:46pm UTC
I came across with a book in which C++ copy assignment operator of a class becomes:
const X &X::operator =(const X&);
Is it correct? (I never saw anything like this, BWT)
Last edited on Aug 4, 2011 at 9:46pm UTC
Aug 4, 2011 at 10:12pm UTC
Yes, it's correct. The compiler doesn't look at the return type when comparing overloads, so that one will look identical to X &X::operator =(const X&);
.
Aug 4, 2011 at 10:29pm UTC
---- [Notice: Post revoked] ----
Wazzak
Last edited on Aug 5, 2011 at 11:21am UTC
Aug 4, 2011 at 10:41pm UTC
Out of interest, which book?
I've always seen the member function version of operator= defined to return a non-const ref.
Last edited on Aug 4, 2011 at 10:42pm UTC
Aug 4, 2011 at 10:48pm UTC
Yes, what book?
I can't imagine why an assignment operator would return a const reference -- that does not make sense on several levels. (IMHO)
Aug 5, 2011 at 12:49am UTC
Yes, the class X has a private pointer member if this information is important:
1 2 3 4 5 6 7 8
class Y;
class X {
public :
const X &operator =(const X&);
private :
Y *mPtr;
};
Based on my testing, it will not compile as the following:
1 2
class X x1, x2, x3;
(x1=x2)=x3;
Now I tend to believe that using
const X &operator =(const X&);
is quite wrong. Anyone think otherwise?
Last edited on Aug 5, 2011 at 1:25pm UTC
Aug 5, 2011 at 1:38am UTC
I have to say, I've never found myself in the situation where I just had to do (a=b)=c
or (a=b).f()
.