C ++运算符重载冲突

时间:2016-06-21 15:37:56

标签: c++ operator-overloading

为什么我没有得到输出。我期待有趣(Test2)称为输出。由于传递的参数对于函数是不同的,因此不应存在任何冲突。

#include <iostream>
using namespace std;
class Test2
{
    int y;
};

class Test
{
    int x;
    Test2 t2;
public:
    operator Test2 ()  { return t2; }
    operator int () { return x; }
};

void fun ( int x) { cout << "fun(int) called"; }
void fun ( Test2 t ) { cout << "fun(Test 2) called"; }

int main()
{
    Test t;
    fun(t);
    return 0;
}

1 个答案:

答案 0 :(得分:3)

您对fun()的来电不明确。 t类型为Test,可以转换为Test2int,因此fun个实现都是候选。