同事去面试的时候的问题:

测试一下发现:在同时存在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环境下同样如此

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-03
  • 2022-02-19
  • 2021-07-18
  • 2022-12-23
  • 2021-06-05
猜你喜欢
  • 2022-12-23
  • 2021-10-04
  • 2021-08-06
  • 2022-12-23
  • 2022-12-23
  • 2021-08-01
  • 2022-02-06
相关资源
相似解决方案