通过传递类的成员变量的名字来对类的成员变量操作

来源:互联网 发布:阿里云解析域名教程 编辑:程序博客网 时间:2024/05/17 20:01

class  A
 {
 public:
  int x;
  int y;
 };
 void f(int A::*member)
 {
  A a;
  a.*member=1;
  cout<<a.y<<endl;
 
 }
int main()
{

 f(&A::y);
 while(1);
}

这种做法适合是类中相同数据类型执行相同操作,这样就可以把需要修改的成员变量的名字传递进去就可以了

0 0