#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