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

Small question about operator overloading

P: n/a
Hello

I'm wondering if it common to return a const reference to an object

const BinSearchTree& operator= (const BinSearchTree& rhs)
throw(bad_alloc);

or

BinSearchTree& operator= (const BinSearchTree& rhs)
throw(bad_alloc);

is there any standard on those

Thanks for any help.
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Alex Snast wrote:
I'm wondering if it common to return a const reference to an object

const BinSearchTree& operator= (const BinSearchTree& rhs)
throw(bad_alloc);

or

BinSearchTree& operator= (const BinSearchTree& rhs)
throw(bad_alloc);

is there any standard on those
There is no standard, but we know that the built-in operator= has the
signature such that it returns a reference to *non-const* object. That
can be used as a hint. The point in most cases is that if you can
assign to that object, you should be able to call a non-const member
function. Example:

struct NormalAssignment
{
void foo() {}
};

struct OpAssReturnsConst
{
OpAssReturnsConst const& operator=(OpAssReturnsConst const&)
{ return *this; }
void foo() {}
};

int main()
{
NormalAssignment n1, n2;
(n1 = n2).foo(); // assign to n1, then call foo() for it.
OpAssReturnsConst c1, c2;
(c1 = c2).foo(); /// OOPS
}

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

P: n/a
On May 30, 5:19 am, Alex Snast <asn...@gmail.comwrote:
Hello

I'm wondering if it common to return a const reference to an object

const BinSearchTree& operator= (const BinSearchTree& rhs)
throw(bad_alloc);

or

BinSearchTree& operator= (const BinSearchTree& rhs)
throw(bad_alloc);

is there any standard on those

Thanks for any help.
Generally, an operator= is defined as:
obj &operator=(const obj &rhs)
{
if(this != &rhs)
{
//do assignment
}
return *this;
}

It returns *this, because this mimics operator= for built-in types.
For example:
int i = 1, j = 2, k = 3;
(i = j) = k; //this is ok

If you return const reference, the above statement will not compile.
Jun 27 '08 #3

P: n/a
Alex Snast <as****@gmail.comwrote:
I'm wondering if it common to return a const reference to an object

const BinSearchTree& operator= (const BinSearchTree& rhs)
throw(bad_alloc);
The above is very uncommon from my experience.
or

BinSearchTree& operator= (const BinSearchTree& rhs)
throw(bad_alloc);

is there any standard on those
I see Stroustrup often uses:

void T::operator=( const T& t );

The the C++ FAQ uses:

T& T::operator=( const T& t );

When the object returned is a reference, it is usually the case that it
returns a reference to *this. As such, it would be pointless to make the
return type a const reference (because the user obviously already has a
non-const reference, value or pointer to the object.)
Jun 27 '08 #4

P: n/a


WaterWalk כתב:
On May 30, 5:19 am, Alex Snast <asn...@gmail.comwrote:
Hello

I'm wondering if it common to return a const reference to an object

const BinSearchTree& operator= (const BinSearchTree& rhs)
throw(bad_alloc);

or

BinSearchTree& operator= (const BinSearchTree& rhs)
throw(bad_alloc);

is there any standard on those

Thanks for any help.

Generally, an operator= is defined as:
obj &operator=(const obj &rhs)
{
if(this != &rhs)
{
//do assignment
}
return *this;
}

It returns *this, because this mimics operator= for built-in types.
For example:
int i = 1, j = 2, k = 3;
(i = j) = k; //this is ok

If you return const reference, the above statement will not compile.
Yeah i know the difference between the two i was just wondering what
to use.

Thanks for your help
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.