I'm trying to overload an operator so I can assign my class to an int like so:
1 2
MyClass a;
int b = a;
I know that I need to use the friend keyword to access the private member of a, but I'm getting some errors that I can't explain.
This is example code: ... replaced with my edit below
EDIT: Okay forget the friend part. This seems just weird:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class MyClass
{
public:
int member;
MyClass() : member(0) {}
};
int& operator = (int &L, MyClass &R)
{
L = R.member;
return L;
}
int main()
{
MyClass a;
int b = a;
}
1>Snippets.cpp(9): error C2801: 'operator =' must be a non-static member
1>Snippets.cpp(17): error C2440: 'initializing' : cannot convert from 'MyClass' to 'int'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Is there a rule against overloading the assignment operator with a basic type on the left or is there something else that I'm missing?
Can someone explain why this works? It seems to work upon both compiling and testing, however I don't understand why I have access to the right-hand-side's private members.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class MyClass
{
int member;
public:
MyClass() : member(0) {}
MyClass& operator = (MyClass &B)
{
member = B.member;
return *this;
}
};
int main()
{
MyClass a, b;
b = a;
}
1>Snippets.cpp(9): error C2801: 'operator =' must be a non-static member
1>Snippets.cpp(17): error C2440: 'initializing' : cannot convert from 'MyClass' to 'int'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
There are a few C++ operators that can only be overloaded as class members:
They are = -> () [] (assignment, pointer to member, function call and array subscript)
As for the other thing - objects of class T has access to any member of of another object of class T - otherwise the whole class thing wouldn't work.
The assignment operator must be a non-static member of the class. It cannot be overridden globally. This applies to all assignment forms (+=, <<=, -=, and so on).
Stewbond wrote:
"I don't understand why I have access to the right-hand-side's private members."
All native members have access to the private and protected parts of the class. This is because functions, operators, and data members are all part of the same class. As to why access is given, I'm not too sure.
Also, a valid overload of the assignment operator for your class must take a reference to a constant class object.
Thanks for the explanation guys! Classes are implicitly friends with themselves. This makes sense and the explicit int cast idea is perfect for my assignment issue.
The call to Money::operator = () will be ambiguous because the return-type alone isn't enough to distinguish between overloads. To disambiguate the call, at least 2 of the following must differ from the rest of the overloads:
Exactly, in what ways do the three overloading of the assignment operators differ from each other??? All the three look the same to me but I suppose I must be wrong?
Neither of the 3 are different; maybe to you, but not the compiler. To differentiate a function from the other overloads, At least two of the following must differ:
The correct implementation of the operator= is to return a reference to the type of the class. So in your original example, you should return a Money&. The point of this is to allow some code like this:
1 2
Money a, b, c;
a = b = c;
If the assignment operator did not return a reference, this wouldn't work.
As for your other question, the this pointer exists to allow you to explicitly reference members of your own class, though as you see it is implied and can be omitted in many (if not all) cases. Another (and IMO more important) reason for it to be there is so you can check for self-assignment by comparing the address of the right-hand side of the operator to this.