Reference, const reference and value parameter

Hey im a bit confused about when to use const and reference parameters etc..

with this definition:

bool operator==(const MyVec& x, const MyVec& y) const;


Am i right in assuming that it will return a boolean that cannot be changed?

And is this probably not a good idea because it might be called by another function. So the correction should be:

bool& operator==(const MyVec& x, const MyVec& y) ;

making the return vale a reference?

Does this mean all overloaded operators will most likely return a ref as they may be passed into other functions?

Thanks
Last edited on
A: Am i right in assuming that it will return a boolean that cannot be changed?
B: Yes
A: And is this probably not a good idea because it might be called by another function
B: ¿Why `another function' needs to change it? (in that case, you could not pass it)
A: So the correction should be making the return value a reference?
B: Nope. ¿Where did you get that reference from?
You will possibly return a reference to a temporary (it is not there anymore)


Note: IIRC, VS allows passing a temporary to function that wants a non-const reference.
Don't know how that "works"

Edit: Your first prototype is incorrect. The const at the end makes no sense.
Last edited on
so would you say:



const bool& operator==(const MyVec& x, const MyVec& y);


would be an appropriate definition?


or even:


const bool operator==(const MyVec& x, const MyVec& y);
Last edited on
basically when overloading assignment and output operators the output and parameters both have to be reference types.

Are there any other "rules of thumb" so to say about when to use reference or const references? Does no one use value?


My question is probably far too broad...
Last edited on
when to use reference or const references?
If you need to modify that variable, then use a reference. If you don't want to modify it, const-reference.

When value is used a copy is made. If that does not cost too much (basic types) or it's what you want (by instance, the algorithm can't be done in-place), use value.


There is the special case of smart pointers. Passing a share pointer by value will permit you to modify the object.
const bool& operator==(const MyVec& x, const MyVec& y);
would be an appropriate definition?

No, of course not. If you have something like this:
1
2
3
4
const bool& operator==(const MyVec& lhs, const MyVec& rhs)
{
  return lhs.x==rhs.x && lhs.y==rhs.y ...;
}

then you'd be returning a reference to a temporary object. It's destroyed at the end of the statement and you'd end up with a dangling reference.

In your second version:
const bool operator==(const MyVec& x, const MyVec& y);
The first const is superfluous, as there is no way you could change the bool that is returned anyway.

Simply returning bool is the correct way to go about this.
Ok thanks alot. I think i get it. You create a reference (essentially a pointer) for no reason in the first, which is not dealt with by destructors. And in the second when using a bool for conditional evaluations there is no way you can change it anyway. One more question about syntax

is there any difference between:

bool operator==(const MyVec& x, const MyVec& y) const;


and

const bool operator==(const MyVec& x, const MyVec& y);

?
Last edited on
Ok thanks alot. I think i get it. You create a reference (essentially a pointer)...


References are aliases, not pointers.
Pointers can do things references cannot.

Pointers can be NULL, whereas references must refer to an object.
Pointers can be assigned, but references can only be initialized.
A reference will always refer to the object with which it is initialized, but a pointer can point to different objects at different times.

is there any difference between:

bool operator==(const MyVec& x, const MyVec& y) const;


and

const bool operator==(const MyVec& x, const MyVec& y);

?


yes. when you learn about classes, the difference between the syntax will become apparent.
So essentially a reference is a const pointer?

Please can you explain the difference have been reading about classes but still do not understand the difference?

So essentially a reference is a const pointer?

Yes I guess... but which behaves as if it's the variable it references.

Please can you explain the difference have been reading about classes but still do not understand the difference?


bool operator==(const MyVec& x, const MyVec& y) const;
This means that the function doesn't change anything inside the class.

const bool operator==(const MyVec& x, const MyVec& y);
This means that the result of this function ought to be treated as const. It usually makes sense doing this when you return a pointer or reference, but here you return a copy. So it doesn't.

http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.10

Edit: tags fix, link.
Last edited on
bool operator==(const MyVec& x, const MyVec& y) const; is invalid.
If you want to make it a global function it should be bool operator==(const MyVec& x, const MyVec& y);
If you want to make it a member bool operator==(const MyVec& y) const;

Athar wrote:
then you'd be returning a reference to a temporary object. It's destroyed at the end of the statement and you'd end up with a dangling reference
I'm a little confused.
I thought that a const reference could be used to extend the life of a temporary,
Thanks catfish.

Why is it invalid ne555? Becuase you can never have a const on the end of global definition?

Also can you never have global overloaded operator definitions with more than two paramenters?
The const at the end is for members. It says that it would not modify *this
It does not make sense in a global function.


You can't change the arity of the operators.
Also can you never have global overloaded operator definitions with more than two paramenters?


As ne555 said, you can't change the arity of operators.
operator== with two parameters is global, operator== with one parameter is not. And that's that.
Well thats that then.
Last edited on
I'm a little confused.
I thought that a const reference could be used to extend the life of a temporary,

You can use it to extend the lifetime of a temporary to the end of the current scope:
1
2
int foo();
const int& x=foo();


Returning a const reference to a local object does not affect its lifetime.
Topic archived. No new replies allowed.