void foo();
class Foomatic() {
void bar();
void baz()
{
std::thread a(foo); // this compiles
std::thread b(Foomatic::bar, this); // this doesn't
std::thread c(&Foomatic::bar, this); // and this compiles
// ...
}
};
我知道成员函数指针的正确语法是&Foomatic::bar
。指向成员函数的指针 - 语法
但是为什么Foomatic::bar
不正确?那个人回来了什么?为什么&Foomatic::bar
是正确的?这对我来说似乎是反直觉的。
这不是重复的。您链接的问题会回答正确的语法,而不是解释原因。
我在问为什么C++在这里如此不一致,我已经知道语法是什么。
@Ed Heal:这不是重复的。我不问,正确的语法是什么,因为我知道它是什么,但是**为什么**是正确的语法。答案只是说明正确的语法。 – marmistrz
答案是重复的。语法不正确,因为语言规范是这样说的。为什么足球比赛长达90分钟?因为这就是规则所说的。 –
也许真正的问题是为什么'a(foo)'有效,当它应该是'(&foo)'? –