【发布时间】:2015-11-08 09:52:12
【问题描述】:
#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> 声明指针时,它就像类成员一样,那么为什么不允许我执行上述程序的 cmets 中给出的那些语句(除了 1)。 px和pxx有什么区别吗(除了scope)?
【问题讨论】:
-
类成员指针的使用方法可以参考stackoverflow.com/questions/670734/…。
-
我之前提到过。我只想知道这个指向成员的指针的范围是什么以及它是否归类为类成员?