I have a following code which is below.
When I write o1 = o2
, void operator=(TestClass& _rhs)
is called. It's ok.
But when I do o1 = test_function();
, first operator float()
is called, then void operator=(float _value)
. It's logically correct, but why is void operator=(TestClass& _rhs)
not invoked?
class TestClass
{
public:
TestClass(float _value)
{
value = _value;
}
operator float()
{
return value;
}
void operator=(float _value)
{
}
void operator=(TestClass& _rhs)
{
}
private:
float value;
};
TestClass test_function()
{
TestClass result = 0;
return result;
}
int main()
{
std::cout << "Hello World!\n";
TestClass o1(1), o2(1);
o1 = o2;
o1 = test_function();
}