Assignment Operator returning a non-reference
Let's say I have the following class:
class A
{
.
.
.
A operator=(const A& rhs);
.
.
.
};
Besides not being able to chain assignment operations, what are the
other negative effects of returning a non-reference from an assignment
operator like this?
# 1 Re: Assignment Operator returning a non-reference
Hi,
one negative effect I could think of is "Efficiency". If your class A is big, say it contains a Meg of bytes, your invocations of the copy constructor become very expensive, because an additional (temporary) object is created when you return the local object "this" (inside your copy-constructor-code).
A A::operator = (const A& rhs)
{
if(this != &rhs)
{
// do the copy of the members of rhs here
this->str = rhs.str; // str 1 megabyte large
}
return *this; // here the copy of str is done again, because a
// temporary object is created
}
# 2 Re: Assignment Operator returning a non-reference
See even if we return a reference then also we have to copy the content of one object to another. Copying of object can't be avoided in any case. so i do not think there is any other negative effect of not returning a reference to the object other than chaining not possible.
# 3 Re: Assignment Operator returning a non-reference
Sorry mate, but what I am saying is, that when you are returning a reference, you have to do the copy once. If you return a non-reference, the copy is done (automatically) once more - hence twice.
Cheers,
D
# 4 Re: Assignment Operator returning a non-reference
Just to clarify my previous mail: The second copy is done using the copy constructor of class A.
And to relativate the whole thing: I guess you can do some clever reference counting to minimize the adverse effect of doing these copies, but that will only really help, if the copied objects are left unchanged after the act...
Cheers,
D
# 5 Re: Assignment Operator returning a non-reference
See even if you do not return anything from the assignment operator say,
void A::operator = (const A& rhs)
{
...return nothing.,
}
even then assignment will take place. so the returned value is not used untill chaining is required so why the constructor would be called when there is no object on the left hand side to take the returned value?
# 6 Re: Assignment Operator returning a non-reference
See this code, compile and execute it. Here Class B defines assignment operator that do return a reference, but class A do return the value (and not the reference)
#include <iostream.h>
#include <stdlib.h>
/*class that retuens reference in assignment operator */
class B{
int i;
public:
B()
{
cout<<"B's Constructor get called :"<<endl;
}
B& operator=(B& rhs)
{
return *this;
}
};
/*class that do not return reference in assignment operator*/
class A{
int i;
public:
A()
{
cout<<"A's Constructor get called :"<<endl;
}
A operator=(A& rhs)
{
// do some assignment operation
return *this;
}
};
int main()
{
A a,b;
a=b;
B c,d;
c=d;
system("pause");
}
In both the cases the constructor is called twice only..
hence it does not make a difference.
Cheers
# 7 Re: Assignment Operator returning a non-reference
Hi mchandel,
Your code is missing the copy constructors for the classes A and B respectively. Bear in mind, that those are AUTOGENERATED if you do not define them yourself! So yes, your default constructors are only called once per object, but in the assignment operator of A the COPY-Constructor is called.
Try and run this code...
#include <iostream.h>
#include <stdlib.h>
/*class that retuens reference in assignment operator */
class B{
int i;
public:
B()
{
cout<<"B's Default Constructor"<<endl;
}
B(const B& rhs)
{
cout<<"B's Copy Constructor"<<endl;
}
B& operator=(B& rhs)
{
cout<<"B's assignment operator"<<endl;
return *this;
}
};
/*class that do not return reference in assignment operator*/
class A{
int i;
public:
A()
{
cout<<"A's Default Constructor"<<endl;
}
A(const A& rhs)
{
cout<<"A's Copy Constructor"<<endl;
}
A operator=(A& rhs)
{
cout<<"A's assignment operator"<<endl;
// do some assignment operation
return *this;
}
};
int main()
{
A a,b;
a=b;
B c,d;
c=d;
return 0;
}
Cheers,
D
# 8 Re: Assignment Operator returning a non-reference
Let's say I have the following class:
class A
{
.
.
.
A operator=(const A& rhs);
.
.
.
};
Besides not being able to chain assignment operations, what are the
other negative effects of returning a non-reference from an assignment
operator like this?
Quite a few:
first of all, you are misleading the innocent reader of your code. Most programmers expect that the assignment operator should return a reference, so they take it for granted that they get a reference, not a value, retruned from this expression. Secondly, some classes can't be constrcuted automatically, or have no public copy ctor so your code will simply not compile. Finally, performance wise returning an object by value is disastrous when for instance the class is question is a vector of strings, because it means that whenever you use the assignment operator, a copy of this huge object is being constructed and destroyed immedialtely afterwards (twice actually).
Danny at 2007-11-11 21:08:27 >

# 9 Re: Assignment Operator returning a non-reference
Thanks for the replies everyone, I know returning a non-reference from an assignment operator is very bad. The reason that I posted this question was that I was helping someone debug their code and they had mistakenly done this, and it was causing a crash, so I was just curious if there were some other strange side-effects that I did not know about. Thanks again.