473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

const MyClass& operator= ... ...

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
4 1816
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nick Savoiu | last post by:
In the code below how can I get x to use the const T& operator? Thanks, Nick #include <stdio.h> template <typename T> class C { public:
20
by: christopher diggins | last post by:
I have heard it is considered good practice to pass function parameters as const& as often as possible, is this true? Is it possible to go overboard? And if so why? Thanks a lot in advance...
12
by: zealotcat | last post by:
template <class T> inline T const& max (T const& a, T const& b) { // if a < b then use b else use a return a<b?b:a; } thanks very much!!
11
by: modemer | last post by:
If I define the following codes: void f(const MyClass & in) {cout << "f(const)\n";} void f(MyClass in) {cout<<"f()\n";} MyClass myclass; f(myclass); Compiler complain that it can't find...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
2
by: Mark Stijnman | last post by:
I would like to be able to have an object accessible as a vector using the operator, but able to track modifications to its data, so that it can update other internal data as needed. I figured...
0
by: tom olson | last post by:
After more searching I found that defining const operators can cause problems with many compilers due to the way it interprets the C++ standard. I removed the const operators from my class and it...
13
by: dragoncoder | last post by:
Hi everyone, please consider the following function:- const int& foo ( const double& d ) { return d; } g++ compiles it with warnings and solaris CC gives error. I want to know if the code...
2
by: ek | last post by:
This first example does not work (cannot be overloaded): int& operator()(int a) { // (1) return a; } int const& operator()(int a) { // (2) return a; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.