同事去面试的时候的问题:
测试一下发现:在同时存在int*和void*的重载函数时,vs2010的环境下,优先匹配void*
1 #include<iostream> 2 using namespace std; 3 4 class Test{ 5 public: 6 int foo(void* y); 7 int foo(int* x); 8 }; 9 10 int Test::foo(void* y) { 11 cout << "void*" << endl; 12 return 0; 13 } 14 15 int Test::foo(int* x) { 16 cout << "int*" << endl; 17 return 0; 18 } 19 20 21 int main() 22 { 23 Test t; 24 int i = 1; 25 t.foo(&i); 26 27 system("pause"); 28 return 0; 29 }
输出:
int*
gcc环境下同样如此