相关疑难解决方法(0)

基于值与Const参考的函数重载

声明如下的内容

void foo(int x)        { std::cout << "foo(int)"         << std::endl; }
void foo(const int &x) { std::cout << "foo(const int &)" << std::endl; }
Run Code Online (Sandbox Code Playgroud)

有意义吗?调用者如何能够区分它们?我试过了

foo(9);  // Compiler complains ambiguous call.

int x = 9;
foo(x);  // Also ambiguous.

const int &y = x;
foo(y);  // Also ambiguous.
Run Code Online (Sandbox Code Playgroud)

c++ overloading pass-by-reference pass-by-value

28
推荐指数
2
解决办法
8372
查看次数