sve*_*nov 2 c++ function functor
有人可以解释一下什么规则确定编译器调用f下面的仿函数而不是函数f?
#include <iostream>
struct A {
void operator()() { std::cout << "functor" << std::endl; }
};
void f() { std::cout << "function" << std::endl; }
int main()
{
A f;
f(); // Output: functor
}
Run Code Online (Sandbox Code Playgroud)
A::operator()()并且f()不是重载,所以我猜这是在重载决策之外发生的.
那是因为名字隐藏.声明f变量时,它会隐藏该f函数.f在该范围内使用该名称将引用局部变量而不是函数.
如果要调用该函数f,则可以使用范围解析运算符:
#include <iostream>
struct A {
void operator()() { std::cout << "functor" << std::endl; }
};
void f() { std::cout << "function" << std::endl; }
int main()
{
A f;
::f(); // Output: function
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
84 次 |
| 最近记录: |