由于标准,指向基数据成员的指针类型
type of pointer to base's data member due to standard
来自以下代码段的 &Derived::member
表达式的类型不是 int Derived:: *
,而是 int Base:: *
(使用 g++5):
#include <iostream>
#include <typeinfo>
struct Base { int member ; } ;
struct Derived : Base {} ;
int main ( )
{
std::cerr << typeid( &Derived::member ).name() ;
return 0 ;
}
根据标准,这是预期的行为吗?
找不到任何东西,可以澄清这个例子。
至少有 two 个类似的问题,但其中 none 个问题需要回答。
g++是对的
来自 C++11 标准:
5.3.1 Unary operators
3 The result of the unary & operator is a pointer to its operand.
...
struct A { int i; };
struct B : A { };
... &B::i ... // has type int A::*
您看到的原因是 int 成员的范围在 Base 而不是 Derived 内。
通过继承,可以访问int成员,但它仍然在Base的范围内。
来自以下代码段的 &Derived::member
表达式的类型不是 int Derived:: *
,而是 int Base:: *
(使用 g++5):
#include <iostream>
#include <typeinfo>
struct Base { int member ; } ;
struct Derived : Base {} ;
int main ( )
{
std::cerr << typeid( &Derived::member ).name() ;
return 0 ;
}
根据标准,这是预期的行为吗? 找不到任何东西,可以澄清这个例子。
至少有 two 个类似的问题,但其中 none 个问题需要回答。
g++是对的
来自 C++11 标准:
5.3.1 Unary operators
3 The result of the unary & operator is a pointer to its operand.
...
struct A { int i; }; struct B : A { }; ... &B::i ... // has type int A::*
您看到的原因是 int 成员的范围在 Base 而不是 Derived 内。
通过继承,可以访问int成员,但它仍然在Base的范围内。