I'm confused over how the overloaded operators decide which argument(s) is/are passed into overloaded operator. I have three examples below which detail three different uses. Hopefully somebody can point me in the right direction.
NOTE: Some of the terminology I use may be incorrect, I am just starting out with C++ so am getting used to it as I go by, and therefore I apologise in advance if my explanations and questions seem confusing, I have tried to explain them as clearly as possible. All of the below code is written purely for examples sake - I do not intend to actually use this code, I have just written it as an example to try and best illustrate where I am confused.
//------------------------------------------
#1): From the example below:
when running in debug mode it is easy to see that 'this' refers to 'e1' and 'other' refers to 'e2', but how is this actually determined? how does the program actually decide which object will be 'this' and which object will be 'other' when passed into the overloaded operator? in this example:
Example e3 = e1 * e2;
is the rule as simple as the argument on the left side of the '*' (e1) becomes 'this' and the argument on the right side of the '*' (e2) is passed into the overloaded operator as the parameter 'const Example& other'?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
class Example
{
public:
int x, y;
Example()
: x(0), y(0) {}
Example(int x, int y)
: x(x), y(y) {}
Example operator*(const Example& other)
{
Example new_obj;
new_obj.x = this->x * other.x;
new_obj.y = this->y * other.y;
return(new_obj);
}
};
int main()
{
Example e1(5, 10);
Example e2(3, 12);
Example e3 = e1 * e2;
std::cout << "e3.x = " << e3.x << std::endl;
std::cout << "e3.y = " << e3.y << std::endl;
// Output:
// e3.x = 15
// e3.y = 120
}
|
//------------------------------------------
#2): From the example below:
I can again see from running the program in debug mode exactly what is happening:
// this:
Example e4 = e1 * e2 * e3;
// is essentially the same as:
Example e4 = e1 * e2;
e4 = e4 * e3;
but again I am confused as to how the decision is actually made in regards to which argument becomes the parameter in the overloaded operator and why.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
class Example
{
public:
int x, y;
Example()
: x(0), y(0) {}
Example(int x, int y)
: x(x), y(y) {}
Example operator*(const Example& other)
{
Example new_obj;
new_obj.x = this->x * other.x;
new_obj.y = this->y * other.y;
return(new_obj);
}
};
int main()
{
Example e1(5, 10);
Example e2(3, 12);
Example e3(4, 11);
Example e4 = e1 * e2 * e3;
std::cout << "e4.x = " << e4.x << std::endl;
std::cout << "e4.y = " << e4.y << std::endl;
// Output:
// e3.x = 15
// e3.y = 120
}
|
//------------------------------------------
#3): From the example below:
similar to the last in that there are 3 arguments - 'std::cout', 'string' and 'std::endl'. but
here it is the '<<' operator which is overloaded. this example confuses me more than the other two because in the line:
std::cout << string << std::endl;
the second '<<' doesn't seem to actually call the overloaded operator as it did in the previous example when the '*' operator was being overloaded. there are two parameters in the overloaded operator this time, one which takes in an 'ostream' and one which takes in an instance of the 'String' class. from this I can infer that std::cout is being passed to the overloaded operator as the 'stream' parameter whilst the 'string' object is passed in by reference. so it is fairly obvious that the operation being performed in the overloaded operator is essentially:
std::cout << string.m_Buffer;
but why does the second '<<' in the line:
std::cout << string << std::endl;
not call the overloaded operator again?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
class String
{
char* m_Buffer;
unsigned int m_Size;
public:
String(const char* string)
{
m_Size = strlen(string);
m_Buffer = new char[m_Size];
memcpy(m_Buffer,string,m_Size);
}
~String()
{
}
friend std::ostream& operator<<(std::ostream& stream, const String& string);
};
std::ostream& operator<<(std::ostream& stream, const String& string)
{
stream << string.m_Buffer;
return(stream);
}
int main()
{
String string("Ryan");
std::cout << string << std::endl;
}
|
//------------------------------------------