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.
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.
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: constbooloperator==(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
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.
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?
booloperator==(const MyVec& x, const MyVec& y) const;
This means that the function doesn't change anything inside the class.
constbooloperator==(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.
booloperator==(const MyVec& x, const MyVec& y) const; is invalid.
If you want to make it a global function it should be booloperator==(const MyVec& x, const MyVec& y);
If you want to make it a member booloperator==(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,
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.