I've been trying to understand operation overloading and the commutative property between classes and fundamental types. I declared the Test to Int asterisk operation but not the Int to Test, just to see if C++ would automatically provide with some sort of commutative function. The testing code is:
#include <iostream>
class Test{
public:
float x;
Test(){
x = 1;
}
Test(float number){
x = number;
}
~Test(){}
};
Test operator*(Test a, int b){
std::cout << "Alert! Test to Int happened! ";
return Test(a.x * b);
}
Test operator*(Test a, Test b){
std::cout << "Alert! Test to Test happened! ";
return Test(a.x * b.x);
}
int main(){
Test my_test;
std::cout << "First result: " << (my_test * 2).x << std::endl;
std::cout << "Second result: " << (3 * my_test).x << std::endl;
return 0;
}
with output:
First result: Alert! Test to Int happened! 2
Second result: Alert! Test to Test happened! 3
The first result was as expected, but for the second result I expected either an error or some unknown-to-me function to do the work. Instead, the Test to Test operation was used... Can someone explain why and what happened?