473,761 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

operator = overloading...

Hi Everyone,

I was overloading the operator= function as a class member function,

#include <iostream.h>

class A
{
int value;
public : A& operator = (const A& ref)
{
this->value = ref.value;
cout<<"in"<<end l;
return *this;
}
A(int value) : value(value)
{
}
A(const A& copy)
{
this->value = copy.value;
cout<<"copy with a value of "<<copy.value<< endl;
}
void print()
{
cout<<"value is"<<value<<end l;
}
};

int main()
{
A obj(0);
A obj1(10);
A obj2(20);
obj.print();
obj1.print();
obj2.print();
obj1 = obj = obj2;
obj.print();
obj.print();
obj2.print();
}

It works fine and all objects have a value of 20 as expected...

Next i changed the return type of the operator = function, like
so that it returns an object instead of a reference...

A operator = (const A& ref)
{
this->value = ref.value;
cout<<"in"<<end l;
return *this;
}

And as the object is returned by value, each call to operator= also
invokes the copy constructor for the temp object, which is again
passed to the operator= function, and the end result happens to be
same as with operator= function which returns a reference.

Is there any thumb rule while overloading operators, when we have
alternative implementations ?

Thanks in advance!!!
Dec 20 '07 #1
7 1743
On Dec 20, 6:43 pm, Rahul <sam_...@yahoo. co.inwrote:
Is there any thumb rule while overloading operators, when we have
alternative implementations ?
Yes. Member operators are generally expected to return a reference to
themselves (at least, that's my undestanding). This provides the
expected behaviour when chaining operators. For example:

MyClass a, b, c;
// Presumably do stuff with c here, then.....
a = b = c;

No extra copies are involved when operator=() returns by reference.

--
Computational Modeling, CSIRO (CMIS)
Melbourne, Australia

Why do you want to return by value from operator=()?
Dec 20 '07 #2
On Dec 20, 1:52 pm, Craig Scott <audiofana...@g mail.comwrote:
On Dec 20, 6:43 pm, Rahul <sam_...@yahoo. co.inwrote:
Is there any thumb rule while overloading operators, when we have
alternative implementations ?

Yes. Member operators are generally expected to return a reference to
themselves (at least, that's my undestanding). This provides the
expected behaviour when chaining operators. For example:

MyClass a, b, c;
// Presumably do stuff with c here, then.....
a = b = c;

No extra copies are involved when operator=() returns by reference.

--
Computational Modeling, CSIRO (CMIS)
Melbourne, Australia

Why do you want to return by value from operator=()?
But the chaining works even when operator= returns a copy too...
Dec 20 '07 #3
On 2007-12-20 02:43:29 -0500, Rahul <sa*****@yahoo. co.insaid:
Hi Everyone,

I was overloading the operator= function as a class member function,

#include <iostream.h>

class A
{
int value;
public : A& operator = (const A& ref)
{
this->value = ref.value;
cout<<"in"<<end l;
return *this;
}
A(int value) : value(value)
{
}
A(const A& copy)
{
this->value = copy.value;
cout<<"copy with a value of "<<copy.value<< endl;
}
void print()
{
cout<<"value is"<<value<<end l;
}
};

int main()
{
A obj(0);
A obj1(10);
A obj2(20);
obj.print();
obj1.print();
obj2.print();
obj1 = obj = obj2;
obj.print();
obj.print();
obj2.print();
}

It works fine and all objects have a value of 20 as expected...

Next i changed the return type of the operator = function, like
so that it returns an object instead of a reference...

A operator = (const A& ref)
{
this->value = ref.value;
cout<<"in"<<end l;
return *this;
}

And as the object is returned by value, each call to operator= also
invokes the copy constructor for the temp object, which is again
passed to the operator= function, and the end result happens to be
same as with operator= function which returns a reference.

Is there any thumb rule while overloading operators, when we have
alternative implementations ?

Thanks in advance!!!
I can't see any legitimate situation where you would prefer to return a
value instead of a reference here.

So, I would say, between those two options, always choose return by
reference since it avoids a copy constructor invocation.

--

-kira

Dec 20 '07 #4
On Dec 20, 10:40 am, Kira Yamato <kira...@earthl ink.netwrote:
On 2007-12-20 02:43:29 -0500, Rahul <sam_...@yahoo. co.insaid:
[...]
Is there any thumb rule while overloading operators, when we
have alternative implementations ?
Thanks in advance!!!
I can't see any legitimate situation where you would prefer to
return a value instead of a reference here.
So, I would say, between those two options, always choose
return by reference since it avoids a copy constructor
invocation.
The general rule is to be as much like the built-in operators as
possible. This basically means that if the built-in operator is
an lvalue, return a reference, and if it is not, return a value.
Since = (and all of the <op>=) are lvalues, you return a
reference.

Note too that if there are implicit conversions to the data
type, you probably want the operators which don't require an
lvalue to be non-members.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 20 '07 #5
On 2007-12-20 04:49:53 -0500, James Kanze <ja*********@gm ail.comsaid:
On Dec 20, 10:40 am, Kira Yamato <kira...@earthl ink.netwrote:
>On 2007-12-20 02:43:29 -0500, Rahul <sam_...@yahoo. co.insaid:

[...]
>>Is there any thumb rule while overloading operators, when we
have alternative implementations ?
>>Thanks in advance!!!
>I can't see any legitimate situation where you would prefer to
return a value instead of a reference here.
>So, I would say, between those two options, always choose
return by reference since it avoids a copy constructor
invocation.

The general rule is to be as much like the built-in operators as
possible. This basically means that if the built-in operator is
an lvalue, return a reference, and if it is not, return a value.
Since = (and all of the <op>=) are lvalues, you return a
reference.

Note too that if there are implicit conversions to the data
type, you probably want the operators which don't require an
lvalue to be non-members.
Can a temporary object be an lvalue? The following code compiles fine
under g++ 4.0.1:

int main()
{
class T {};
T() = T();
return 0;
}

--

-kira

Dec 20 '07 #6
On Dec 20, 11:53 am, Kira Yamato <kira...@earthl ink.netwrote:
On 2007-12-20 04:49:53 -0500, James Kanze
<james.ka...@gm ail.comsaid:
On Dec 20, 10:40 am, Kira Yamato <kira...@earthl ink.netwrote:
On 2007-12-20 02:43:29 -0500, Rahul <sam_...@yahoo. co.insaid:
[...]
>Is there any thumb rule while overloading operators, when we
have alternative implementations ?
>Thanks in advance!!!
I can't see any legitimate situation where you would prefer to
return a value instead of a reference here.
So, I would say, between those two options, always choose
return by reference since it avoids a copy constructor
invocation.
The general rule is to be as much like the built-in operators as
possible. This basically means that if the built-in operator is
an lvalue, return a reference, and if it is not, return a value.
Since = (and all of the <op>=) are lvalues, you return a
reference.
Note too that if there are implicit conversions to the data
type, you probably want the operators which don't require an
lvalue to be non-members.
Can a temporary object be an lvalue?
Not normally. The standard doesn't actually make an explicit
equivalence, but most of the cases listed in §12.2 where a
temporary comes into existence involve expressions which are
rvalues.
The following code compiles fine under g++ 4.0.1:
int main()
{
class T {};
T() = T();
return 0;
}
Yes. The reason is that there is no problem calling a member
function on a temporary, and for class types, operator= is a
member function.

To return to my initial point: there is no way to restrict a
user defined operator to lvalues. By making it a member,
however, you do prevent conversions: built-in operators
which require lvalues will not accept the results of
conversions, and those that do not require lvalues do.

At least, that's the commonly accepted point of view. On
thinking about it, however: if you define something like
operator+= as a non-member (friend), then the first argument
will be a non-const reference (otherwise you cannot possibly
give it the semantics it should have). And you can't initialize
a non-const reference with a temporary. Which would argue for
making operators requiring an lvalue free functions, and not
members. (Except for operator=, which for other reasons cannot
be a free function.)

Note that the issue is far from moot. Suppose you want an
iterator to the last element in a container you know is not
empty, and that has at least a bidirectional iterator (e.g. any
of the standard sequences). You might be tempted to write:
-- container.end()
. If the operator-- function of the iterator is a member (which
it is in most, if not all, current implementations ), this works.
If it is not (or if the "iterator" is just a typedef for a
pointer, which was the case in a lot of early implementations of
std::vector), then it doesn't. (Since the abstraction of STL
iterators is a pointer, we'd really like for it not to work.)

So it's worth thinking about, even if the currently accepted
best practice is as I originally stated: that operators which
require lvalues be members, and operators that don't be free
functions. (And of course, when you deviate from currently
accepted best practices, aka the standard way of doing things,
you raise questions in your readers' minds. It's better not to
without very strong reasons.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 21 '07 #7
On 2007-12-21 04:27:06 -0500, James Kanze <ja*********@gm ail.comsaid:
On Dec 20, 11:53 am, Kira Yamato <kira...@earthl ink.netwrote:
>On 2007-12-20 04:49:53 -0500, James Kanze
<james.ka...@g mail.comsaid:
>>On Dec 20, 10:40 am, Kira Yamato <kira...@earthl ink.netwrote:
On 2007-12-20 02:43:29 -0500, Rahul <sam_...@yahoo. co.insaid:
>>[...]
Is there any thumb rule while overloading operators, when we
have alternative implementations ?
>>>>Thanks in advance!!!
>>>I can't see any legitimate situation where you would prefer to
return a value instead of a reference here.
>>>So, I would say, between those two options, always choose
return by reference since it avoids a copy constructor
invocation .
>>The general rule is to be as much like the built-in operators as
possible. This basically means that if the built-in operator is
an lvalue, return a reference, and if it is not, return a value.
Since = (and all of the <op>=) are lvalues, you return a
reference.
>>Note too that if there are implicit conversions to the data
type, you probably want the operators which don't require an
lvalue to be non-members.
>Can a temporary object be an lvalue?

Not normally. The standard doesn't actually make an explicit
equivalence, but most of the cases listed in §12.2 where a
temporary comes into existence involve expressions which are
rvalues.
>The following code compiles fine under g++ 4.0.1:
>int main()
{
class T {};
T() = T();
return 0;
}

Yes. The reason is that there is no problem calling a member
function on a temporary, and for class types, operator= is a
member function.

To return to my initial point: there is no way to restrict a
user defined operator to lvalues. By making it a member,
however, you do prevent conversions: built-in operators
which require lvalues will not accept the results of
conversions, and those that do not require lvalues do.

At least, that's the commonly accepted point of view. On
thinking about it, however: if you define something like
operator+= as a non-member (friend), then the first argument
will be a non-const reference (otherwise you cannot possibly
give it the semantics it should have). And you can't initialize
a non-const reference with a temporary. Which would argue for
making operators requiring an lvalue free functions, and not
members. (Except for operator=, which for other reasons cannot
be a free function.)

Note that the issue is far from moot. Suppose you want an
iterator to the last element in a container you know is not
empty, and that has at least a bidirectional iterator (e.g. any
of the standard sequences). You might be tempted to write:
-- container.end()
. If the operator-- function of the iterator is a member (which
it is in most, if not all, current implementations ), this works.
If it is not (or if the "iterator" is just a typedef for a
pointer, which was the case in a lot of early implementations of
std::vector), then it doesn't. (Since the abstraction of STL
iterators is a pointer, we'd really like for it not to work.)

So it's worth thinking about, even if the currently accepted
best practice is as I originally stated: that operators which
require lvalues be members, and operators that don't be free
functions. (And of course, when you deviate from currently
accepted best practices, aka the standard way of doing things,
you raise questions in your readers' minds. It's better not to
without very strong reasons.)
Very interesting read.

So, this is telling me that we should do things in your suggested way:
that is, declare operators that should require lvalues be free
functions and declare its first parameter a non-const reference.

Well, even if it is not standard practice, I'll start doing that.

--

-kira

Dec 21 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
2617
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class, but I would like to use internally my own = operator for some of my value types, so I can say "x = y;" rather than "x.Assign(y);". The op_Assign operator seems impossibly broken since it takes __value copies of the two objects. Perhaps there is...
34
6468
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment operator. Here's what I'm trying to do. I've defined class Complex as class Complex { friend ostream &operator<<( ostream &, Complex & ); public: Complex( float = 0.0, float = 0.0 );
16
3094
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class has two int memebers "dataA", "dataB". The derived class has an additional int member "dataC". I am simply trying to overload the + operator so that 'adding' two objects will sum up the corresponding int members.
2
2067
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the overloading of operator<<. In the .cc of the respective class or in a file where I am overloading operator<< for all classes? Cheers,
67
8660
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used in low-level numerical computations, replacing Fortran in newer projects. Check, for example, sourceforge.net or freshmeat.net But neither language offers a primitive exp operator.
3
2299
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
5
3629
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), , ->, =. I wonder why there is such a restriction. Some tutorials say that 'new' and 'delete' can only be overloaded with static member functions, others say that all overloading function should be non-static. Then what is the fact, and why? ...
3
3280
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj, obj2 ;
9
3513
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by allowing the assignment operator (=) to be overloaded. One particular use for this would be to implement "lazy evaluation". For example it would allow us to get rid of all the temporary arrays produced by NumPy. For example, consider the...
8
2975
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular, operator =, operator, operator(), and operator-must be nonstatic member function; this ensures that their first operands will be lvalues". I know that these operators must be nonstatic member functions, but why this ensure their first operands will...
0
9538
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10123
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9975
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9909
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9788
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8794
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7342
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5241
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3481
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.