operator overloading question

J

Jaap Versteegh

I have overloaded the addition operator for a class like this:
=====
namespace Geom {

class Node {
...
}

Node operator+ (Node& n1, Node& n2)
{
...
}

void f()
{
Node t1;
Node t2;
Node t3;
Node t4;
t4 = t1 + t2; // ok
t4 = t1 + t2 + t3; //error
}

}
=========
It produces the following error:

error: no match for 'operator+' in
'Geom::eek:perator+(Geom::Node&,Geom::Node&)((&t2)) + t3'

Could someone please explain to me what the problem is and how to solve it ?

TIA,

Jaap Versteegh
 
A

Amit

Jaap Versteegh said:
I have overloaded the addition operator for a class like this:
=====
namespace Geom {

class Node {
...
}

Node operator+ (Node& n1, Node& n2)
{
...
}

Try returning a reference to the Node object. Reference acts as a lvalue and
that would help further cascading.
 
P

Pete Becker

Amit said:
Try returning a reference to the Node object. Reference acts as a lvalue and
that would help further cascading.

And what object would that reference refer to? <g>

The solution is to pass the arguments by const&.
 
V

Victor Bazarov

Amit said:
Try returning a reference to the Node object. Reference acts as a lvalue and
that would help further cascading.

That's not a good idea. What would the reference refer to? A local
object?

A better solution would be to accept the arguments as references to const
objects:

Node operator+ (Node const & n1, Node const & n2)

V
 
V

Victor Bazarov

Jaap said:
Thank you, that fixed it, though I don't understand why....

When you return an object, it's a temporary. When you need to pass it as
one of the arguments to another operator+, a reference to non-const cannot
be the argument type, the language prohibits binding a non-const reference
to a temporary. A const reference (or a reference to const, as it is more
correctly known) is allowed to bind to a temporary.

Read more about temporaries and const-correctness.

V
 
J

Jaap Versteegh

Victor said:
Read more about temporaries and const-correctness.

Thank you for this advice.

Modifying a temporary is useless, because the effect will be discarded
anyway. So trying to change its value might be an error and therefore the
compiler only allows a constant reference to it.
It's not that the compiler CAN'T handle it, but it refuses to.

This concept is a bit new to me. As you understood, I am relatively new to
C++, having done mostly Delphi. But I like this, it's nice :)

Regards,

Jaap Versteegh
 
V

Victor Bazarov

Jaap said:
[..]
Modifying a temporary is useless, because the effect will be discarded
anyway. So trying to change its value might be an error and therefore
the compiler only allows a constant reference to it.

That's not necessarily true. You are allowed to call a non-const member
function for a temporary.
It's not that the compiler CAN'T handle it, but it refuses to.

I think there are other considerations. I don't remember which, though.
This concept is a bit new to me. As you understood, I am relatively new
to C++, having done mostly Delphi. But I like this, it's nice :)

There are many more things for you to discover in C++. You'll like 'em.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top