假设我有:
// This is all valid in C++11.
struct Foo {
int i = 42;
int& j = i;
};
// Let's take a pointer to the member "j".
auto b = &Foo::j; // Compiler is not happy here
// Note that if I tried to get a pointer to member "i", it would work, as expected.
Foo f;
std::cout << f.*b; // Try using the pointer to member
编译器抱怨我不能获取成员的地址,因为它是一个引用。准确地说:
语义问题:无法形成指向成员的指针,指向引用类型为“int &”的成员“j”
我知道这样做似乎毫无意义,但我只是想知道为什么不能这样做。
为什么这是不可能的?