I'm noticing it's outputting 60 as the result though. Is this because it's not calling operator=?
As was previously discussed, this line:
ref = yourApple;
copies yourApple into ref. But, ref can be thought of as a synonym(=an alternate name) for myApple because of this line:
Apple& ref = myApple;
so the line:
ref = yourApple;
changed myApple's member variable to 30, and when you add myApple and yourApple you get 60. Sorry, I should have gotten rid of the reference stuff in main() left over from the previous example.
Here is a clearer example of the operator+ in action:
Code:
#include <iostream>
using namespace std;
class Apple
{
public:
int num;
Apple(){}
Apple(int n)
{
num = n;
}
Apple operator+(const Apple& rhs) const
{
return Apple(num + rhs.num);
}
Apple& operator=(const Apple& rhs)
{
if(this == &rhs) //checks to see if lhs and rhs are the same objects
{
cout<<"they're equal, exiting function directly"<<endl;
return *this; //if lhs==rhs, then executing the code below is unnecessary
}
cout<<"assignment operator called"<<endl;
num = rhs.num;
return *this;
}
};
int main()
{
Apple myApple;
myApple.num = 10;
Apple yourApple;
yourApple.num = 30;
Apple newApple = myApple + yourApple;
cout<<endl<<newApple.num<<endl;
//tests new portion of operator=
Apple& ref1 = myApple;
Apple& ref2 = myApple;
ref1 = ref2;
return 0;
}
Note: I added a new portion to the operator= function. You should make that check, so that you don't execute a bunch of code unnecessarily.
I'm guessing [the operator=] wasn't called because...
Sorry, I can't explain it either. I thought I would be able to. I presumed another of the default functions the compiler supplies, namely the default copy constructor, was invisibly executing behind the scenes instead of the operator= function, but when I explicitly defined a copy constructor for the Apple class, it wasn't called either. I put display messages in the default constructor and the int constructor as well, and none of them displayed a message. So, it appears newApple is being created without a call to a constructor, which is impossible. One of the pros will have to weigh in on that, but I doubt any of them will wade through all these posts and end up here. I'll think about it some more, and if I figure it out, I'll post what's happening--otherwise I'll ask the pros by starting another thread.
And you said you can return any type, such as an int. But doesn't it know to call the operator in the class by the lhs? By assigning an something to an Apple?
Sorry, I can't make out what you are saying there. But, if you define your operator= function like this:
Code:
int operator=(const Apple& anApple)
{
cout<<"Apple to int assignment called"<<endl;
return anApple.num;
}
then you have to use it like this:
Code:
Apple myApple;
myApple.num = 5;
Apple yourApple;
yourApple.num = 25;
int appleNum = myApple = yourApple;
That last line is equivalent to:
int appleNum = (myApple = yourApple);
and the way it's executed goes like this: myApple calls the operator= method that takes an Apple object as a parameter. That method returns an int. So, you replace the part that's enclosed in parentheses with the int to produce:
int appleNum = (25);
Your attempt looks like this:
int* appleNum = myApple;
Remember, the left hand side is the object that is calling the operator= method. Is appleNum an object of the Apple class? The answer is no, so appleNum can't call any method in the Apple class. So, the compiler looks around outside the Apple class for an operator= method defined for the int* class that takes an Apple object as a parameter(that's what's on the rhs of the equals sign), and can't find anything, so you get an error.