例如,我有一个内部类:
struct Foo {
void test() {}
};
和一个外部类:
struct Bar {
Foo foo;
};
然后在main()
中
Bar bar{};
Foo Bar::* pFoo = &Bar::foo;
bar.*pFoo.test(); // does not work
Foo foo = bar.*pFoo;
foo.test(); // works;
关于bar.*pFoo.test()
的错误是:member reference base type 'Foo Bar::*' is not a structure or union
,那么bar.*pFoo.test();
和Foo foo = bar.*pFoo; foo.test();
之间有什么区别?
答案 0 :(得分:3)
如评论中所暗示,.*
的运算符优先级比.
更低。
因此bar.*pFoo.test();
被解析为bar.*(pFoo.test());
,并试图访问test
的{{1}}成员。
由于pFoo
是类型pFoo
的成员指针,因此这不是有效的表达式。成员访问运算符Foo Bar::*
的左侧只能显示类类型(不包括伪析构函数调用语法),而成员指针则不能。
第二个示例等效于表达式.
。