By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,669 Members | 1,645 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,669 IT Pros & Developers. It's quick & easy.

const MyClass& operator= ... ...

P: n/a
Was wondering why the function that overloads = operator, returns always
a const reference to the class.

Why the qualifier 'const'?

Thanks in advance

Jul 9 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
nembo kid wrote:
Was wondering why the function that overloads = operator, returns always
a const reference to the class.

Why the qualifier 'const'?
First of all, the premise is untrue. The compiler-provided operator=
does not return the reference to a const object. It returns a reference
to a non-const object.

Second, if one of _your_ classes returns a reference to const, they you
need to ask yourself (or whoever gave you the class) why the normal
(idiomatic) interface was not followed.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 9 '08 #2

P: n/a
Victor Bazarov wrote:
nembo kid wrote:
>Was wondering why the function that overloads = operator, returns
always a const reference to the class.

Why the qualifier 'const'?

First of all, the premise is untrue. The compiler-provided operator=
does not return the reference to a const object. It returns a reference
to a non-const object.

Second, if one of _your_ classes returns a reference to const, they you
need to ask yourself (or whoever gave you the class) why the normal
(idiomatic) interface was not followed.
In fact, the operator=() can return *anything* (including void).
However, if you return anything else than a reference to self, your
class cannot be used in the traditional way with assignments (except
with a simple "a = b;").
Jul 9 '08 #3

P: n/a
Juha Nieminen ha scritto:
In fact, the operator=() can return *anything* (including void).
However, if you return anything else than a reference to self, your
class cannot be used in the traditional way with assignments (except
with a simple "a = b;").
Maybe that when operator=() makes a rvalue it returns a const, while
when it makes a lvalure it returns a non const??

Example

MyClass a, b, c;

I would avoid this assignment (a = b) as rvalue:

(a = b) = c;

Jul 10 '08 #4

P: n/a
On Jul 9, 7:21 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Victor Bazarov wrote:
nembo kid wrote:
Was wondering why the function that overloads = operator,
returns always a const reference to the class.
Why the qualifier 'const'?
First of all, the premise is untrue. The compiler-provided
operator= does not return the reference to a const object.
It returns a reference to a non-const object.
Second, if one of _your_ classes returns a reference to
const, they you need to ask yourself (or whoever gave you
the class) why the normal (idiomatic) interface was not
followed.
In fact, the operator=() can return *anything* (including
void). However, if you return anything else than a reference
to self, your class cannot be used in the traditional way with
assignments (except with a simple "a = b;").
That's not quite true. A const reference return type will
support all of the traditionan C uses of assignment. (In C,
assignment was not an lvalue.) Where it breaks are things like:

T&
someFunction( /* ... */ )
{
// ...
return a = b ;
}

Arguably, such idioms aren't good programming practice, and
should be avoided anyway. But they work with built-in types and
with the compiler generated default operator, and it seems best
that user defined operators adher as close as possible to the
way the built-in operators work.

One notable exception to this is proxy objects, where the
operator= itself is usually const.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 10 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.