IainHere
asked on
operator=(const &myclass)
I've just spent a couple of hours tracking down an error, which turned out to be that a copy assignment operator was mistyped - what should have been
myclass& myclass::operator=(const myclass& m);
was actually written as
myclass& myclass::operator=(const &myclass m);
My question is - why did the second version compile at all? Does it mean anything?
myclass& myclass::operator=(const myclass& m);
was actually written as
myclass& myclass::operator=(const &myclass m);
My question is - why did the second version compile at all? Does it mean anything?
const& myclass m would mean "m is a const reference to a myclass". But that is redundant, since references are always const. You can't change a reference. Never.
In other words, "myclass& const m" is the same thingequivalent to "myclass& x". Since you're gaining nothing by adding the const after the &, you shouldn't add.
So it is actually suprising that your compiler didn't complain, because this constact is nonsence.
ASKER
Dan - my apologies. It was actually
CMyClass& operator=(const &CMyClass)
the operator was declared as private to stop anything using the default one, so the parameter wasn't actually being used so it wasn't named.
mblat - "references are always const". I think I must be misunderstanding what you're saying - references don't have to to be const - if you pass them non-const, you can alter them as if they were pointers, but with object semantics. Am I missing something? Anyway, I'm sure the two things you state are equivalent are not.
CMyClass& operator=(const &CMyClass)
the operator was declared as private to stop anything using the default one, so the parameter wasn't actually being used so it wasn't named.
mblat - "references are always const". I think I must be misunderstanding what you're saying - references don't have to to be const - if you pass them non-const, you can alter them as if they were pointers, but with object semantics. Am I missing something? Anyway, I'm sure the two things you state are equivalent are not.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent! Thanks for the description.
[apologies for leading you the merry dance, Dan - I'm sure you'll forgive me]
[apologies for leading you the merry dance, Dan - I'm sure you'll forgive me]
>>Dan - my apologies. It was actually CMyClass& operator=(const &CMyClass)
Well that is irritating beyond words. But I accept your appology :-)
-- Dan
Well that is irritating beyond words. But I accept your appology :-)
-- Dan
class CMyClass {
public:
int n;
CMyClass& operator=(const &CMyClass m);
};
returns:
error C2146: syntax error : missing ',' before identifier 'm'
error C2061: syntax error : identifier 'm'
-- Dan