奇怪的语法:作用域运算符(::)后面有星号?
帮助我理解以下代码片段:
(foo.h)
class Foo
{
public:
typedef void (MyType::*Handler)(SomeOtherType* t);
Foo(Handler handler) : handler_(handler) { }
private:
Handler handler_;
};
(mytype.h)
class MyType
{
public:
MyType() { }
void fun1() { }
void fun2() { }
};
foo.h 中的 typedef 在这里声明的到底是什么?我可以看到它是某种函数指针,但是星号的意义是什么?它似乎正在取消对类型的引用(??),并以某种方式尝试将新的 typedef 指针“附加”到 MyType 的类型(?!?)。
有人可以在这里解释一下吗?真的很困惑:S
Help me understand the following code snippet:
(foo.h)
class Foo
{
public:
typedef void (MyType::*Handler)(SomeOtherType* t);
Foo(Handler handler) : handler_(handler) { }
private:
Handler handler_;
};
(mytype.h)
class MyType
{
public:
MyType() { }
void fun1() { }
void fun2() { }
};
What exactly is the typedef in foo.h declaring here? I can see that it's a function pointer of some kind but what's the significance of the asterisk? It appears to be de-referencing a type (??) and somehow trying to "attach" the newly typedef'd pointer to the type of MyType (?!?).
Can someone shed some light here please? Really confused :S
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
void (MyType::*)(SomeOtherType* t)
是一个指向类MyType
中成员函数的指针,该函数采用一个参数(指向SomeOtherType
的指针) >) 并且什么也不返回。FAQ Lite 条目。
void (MyType::*)(SomeOtherType* t)
is a pointer to a member function in classMyType
that takes one argument (pointer toSomeOtherType
) and returns nothing.FAQ Lite entry.
指向
MyType
成员函数的指针,返回void
并将指向SomeOtherType
的指针作为参数。Pointer to a
MyType
member function returningvoid
and taking pointer toSomeOtherType
as a parameter.