您的位置:首页 > 其它

用函数指针和指针引用类中的成员和变量

2013-08-30 13:09 239 查看
class Y
{
public:
int x;
static int x_s;
static void Def_S()
{
cout<<"OK"<<endl;
}
void Def()
{
cout<<"KO"<<endl;
}
};

int Y::x_s = 10;
int main()
{
//静态函数引用
void (*ptr)() = & Y::Def_S;
ptr();

//非静态函数引用
Y y;
void (Y::*ptr_s)() = &Y::Def;
(y.*ptr_s)();

//静态变量引用
cout<<"静态变量引用前 Y::x_s:" << Y::x_s <<endl;
int *p_s = &Y::x_s;
*p_s = 20;
cout<<"静态变量引用后 Y::x_s:" << Y::x_s <<endl;

//非静态变量引用
y.x = 30;
cout<<"非静态变量引用前 Y::x_s:" << y.x <<endl;
int Y::*p = &Y::x;
y.*p = 40;
cout<<"非静态变量引用后 Y::x_s:" << y.x <<endl;

return -1;
}

结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: