在
http://topic.csdn.net/u/20071203/19/47c033a5-6501-49c1-ac1b-ead5cadb0fca.html?seed=50094126
看到
include <iostream>
using namespace std;
class CText
{
public:
const int a;
CText();
};
CText::CText():a(0)
{
}
int main()
{
CText t;
int const CText::*p;
p=&CText::a;
int CText::*q;
q=(int CText::*)p;
t.*q=10;
cout < <t.*q < <endl;
return 0;
}
这样的程序,不明白
CText t;
int const CText::*p;
p=&CText::a;
是什么意思?C++中可以这样定义变量吗?
恕我无知,以前从没见过……
总的来说p其实代表的就是一个偏移量
p=&CText::a; //在类CText中把变量a的偏移量给p
现在通过变量p就可以直接操作某一个CText对象的成员变量a。
CText t;
t.*p = 10; //.*操作符,相当于t.a = 10
CText *pt = &t;
pt-> *p = 10; //-> *操作符,也相当于t.a = 10
x谢谢jxlczjp77。
看到
include <iostream>
using namespace std;
class CText
{
public:
const int a;
CText();
};
CText::CText():a(0)
{
}
int main()
{
CText t;
int const CText::*p;
p=&CText::a;
int CText::*q;
q=(int CText::*)p;
t.*q=10;
cout < <t.*q < <endl;
return 0;
}
这样的程序,不明白
CText t;
int const CText::*p;
p=&CText::a;
是什么意思?C++中可以这样定义变量吗?
恕我无知,以前从没见过……
- jxlczjp77 回复道:
总的来说p其实代表的就是一个偏移量
p=&CText::a; //在类CText中把变量a的偏移量给p
现在通过变量p就可以直接操作某一个CText对象的成员变量a。
CText t;
t.*p = 10; //.*操作符,相当于t.a = 10
CText *pt = &t;
pt-> *p = 10; //-> *操作符,也相当于t.a = 10
x谢谢jxlczjp77。