简体   繁体   English

为什么成员函数需要'&'(例如在std :: bind中)?

[英]Why does a member function needs '&' (e.g. in std::bind)?

When I was playing with std::bind from the C++11 -standard I recognized the following would be allowed by the compiler: 当我从C ++ 11- standard开始使用std::bind ,我发现编译器允许以下内容:

class Foo
{
public:
  void F();
  int G(int, int);
};

void Foo::F()
{
  auto f = bind(&Foo::G, this, _1, _2);
  cout << f(1,2) << endl;
}

int Foo::G(int a, int b)
{
  cout << a << ',' << b << endl;
  return 666;
}

But if I eliminated the '&' in front of the Foo::G in the bind -line, I would get some compiler errors (using MinGW 4.7). 但是如果我在bind消除了Foo::G前面的'&',我会得到一些编译器错误(使用MinGW 4.7)。

Why is Foo::G not valid as a pointer to a member function, although H and &H would both work for "usual" functions? 为什么Foo::G无效作为指向成员函数的指针,尽管H&H都适用于“通常”函数?

LG ntor LG ntor

& is required to take address of a member function, some compilers will allow you to omit the same but it is non-standard and at times confusing. &需要获取成员函数的地址,一些编译器将允许您省略相同但非标准并且有时令人困惑。

you can read about member function pointers in here: http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible 你可以在这里阅读有关成员函数指针的内容: http//www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible

I take it that one of the reasons could have been consistency. 我认为其中一个原因可能是一致性。 Recall that within a class, you can say 回想一下,在课堂上,你可以说

MyClassOrOneOfItsBases::memberFunction();

It compiles fine, since the qualified name names the member function, instead of forming a pointer to member. 它编译得很好,因为限定名称命名成员函数,而不是形成指向成员的指针。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较函数放在哪里(例如std :: sort)? - Where to put comparison function for use with (e.g.) std::sort? 一系列相关函数的通用函数(例如std :: stoi,std :: stof,std :: stod等) - Generic function for a family of related functions (e.g. std::stoi, std::stof, std::stod etc) 隐式函数到函数指针转换不适用于标准容器(例如向量) - Implicit function-to-function-pointer conversion not working for std containers (e.g. vector) 没有匹配的 function 来调用:错误:必须使用 '.*' 或 '-&gt;*' 来调用指向成员 function 在 'f (...)' 中的指针,例如 '(... -&gt;* f) (...)' - No matching function to call: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘f (…)’, e.g. ‘(… ->* f) (…)’ 可以包装可调用对象的文字类型,例如 function 或成员 function - Literal type that can wrap a callable, e.g., function or member function 为什么不能使用auto作为模板类型参数(例如std :: array <auto, 5> )? - Why can't you use auto as a template type argument (e.g. std::array<auto, 5>)? 从函数返回const char *的正确方法,例如,重写std :: exception :: what() - Proper method of returning const char* from a function, e.g., overriding std::exception::what() 使用enable_if和SFINAE时函数参数类型推导(std容器,例如vector)失败 - function argument type deduction (std container e.g. vector) fails when using enable_if and SFINAE 标准中的哪里是模板参数的语法,例如,`std::function<int(char)> `定义?</int(char)> - Where in the standard is the grammar for the template argument in, e.g., `std::function<int(char)>` defined? 模板 arguments 中的括号,例如 std::function<int(int, float)></int(int,> - Parentheses inside template arguments e.g. std::function<int(int, float)>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM