用函数指针和指针引用类中的成员和变量
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; }
结果:
相关文章推荐
- 关于类成员变量为引用与指针的思考
- 指向结构体变量的指针引用结构体变量中的成员
- 指向结构体变量的指针引用结构体变量的成员
- 类成员变量中存在引用,const,和指针类型时需要注意的事项
- 指向结构体变量的指针引用结构体变量的成员
- 用引用计数管理指针成员变量
- java 中的 成员变量、局部变量、静态变量、类变量、非静态变量、实例变量、向前引用、非法向前引用、静态代码块、非静态代码块 执行时机
- 编程题:对结构体变量中成员的引用展示。
- C++中的线程函数如何访问类中的成员变量
- C++ 类中特殊的成员变量(常变量、引用、静态)的初始化方法
- 为什么C++中千万不要返回局部对象或变量的引用和指针
- 函数指针,成员函数指针,重载函数指针
- C++智能指针作为成员变量的用法
- 关于 类成员函数返回指向const临时变量的引用
- 指针 和引用 || 静态成员函数 || 虚函数 || 系统调用和库函数 ||
- c++高级---C++ 类中特殊的成员变量(常变量、引用、静态)
- C++ 类中特殊的成员变量(常变量、引用、静态)的初始化方法
- 被引用对象的类型而不是引用变量的类型决定了调用谁的成员方法
- 返回指向局部变量的指针或引用的问题
- 有指针成员变量,要注意莫要MLK了