#include <iostream>
using namespace std;
class Sample
{
public:
int *pxx;
int x;
void setD(int y)
{
x=y;
}
void print()
{
int Sample::*px = &Sample :: x;
cout<<"\nx : "<<x;
cout<<"\nAddress of x : "<<&x;
cout<<"\nValue of X indirected through px :"<<this->*px;
//cout<<"\nValue of X indirected through px :"<<*px; ERROR
//cout<<"\nAddress of x i.e. px : "<<px; NO ERROR BUT UNDESIRED OUTPUT(The output is most of the time '1')
//cout<<"\nAddress of x i.e. px : "<<this->px; ERROR
}
};
我读过,当指针使用语法data type <class_name> :: * <pointer> = &<class_name> :: <variable_name>
它就像一个类成员声明,那么我为什么不允许执行上述计划的意见给出的声明(1除外)。 px和pxx之间是否有区别(范围除外)?指针在C++成员
你可以参考http://stackoverflow.com/questions/670734/c-pointer-to-class-data-member有关如何使用指向类成员的指针。 – user258367
我之前提到过它。我只想知道这个_Pointer的范围是什么,并且它是否被分类为**类成员**? – Bateman