这个函数是否覆盖了函数?
void f(int a, int b) {}
void f(int& a, int& b) {}
void f(int* a, int* b) {}
void f(const int a, const int b) {}
Run Code Online (Sandbox Code Playgroud)
或者只有当参数的数量或类型不同时才会发生重载功能?
1号,2号,3号是.4号重新宣布1号.
你看,顶级const限定符不影响函数声明.它们改变了在函数定义中使用它们的方式,但它仍然是相同的函数
void f(int);
void f(const int); //redeclares
void f(const int x) //same as above
{
x = 4; //error, x is const
}
Run Code Online (Sandbox Code Playgroud)
另一方面,这很好
void f(const int);
void f(int x) //same as above
{
x = 4; //OK Now
}
Run Code Online (Sandbox Code Playgroud)
请注意,非顶级consts确实参与了重载决策.例如
void f(int & x)
void f(const int & x) //another function, overloading the previous one
Run Code Online (Sandbox Code Playgroud)
最后,你在问
或者只有当参数的数量或类型不同时才会发生重载功能?
是的,这是正确的,但是int,int*和int&是不同的类型.当然,在1或3的情况下,在大多数情况下,它将是不明确的调用,但在某些情况下,编译器可以告诉您的意思.例如
void f(int);
void f(int&);
f(3)//calls f(int)
int x = 3;
f(x); //ambiguous call;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
226 次 |
| 最近记录: |