main.cpp: In function 'int main()':
main.cpp:41: error: invalid cast from type 'int A::*' to type 'int'
main.cpp:42: error: invalid cast from type 'int A::*' to type 'int'
c++ primer: 13.6.2
类成员的指针必须总是通过特定的对象或指向该类类型的对象的指针来访问。
class Derived :
public Base1,
public Base2 {
public:
int value3;
};
int _tmain(int argc, _TCHAR* argv[])
{
printf("\n Point to class's data member %p.\n", &Derived::value3);//第一种情形
printf("\n Point to class's data member %p.\n", &Derived::value2);//It should be 4,but it is 0.
int Derived:: *pVal3 = &Derived::value3;//第二种情形
Derived object;
object.*pVal3 = 5;
printf("\n Point to object's data member %p, value is %d.\n", &(object.*pVal3), object.*pVal3);