#include<iostream> using namespace std; class Integer { public: void Display(); static void Fun(int num1,int num2,Integer *ptr); private: int member_int; static int static_int; }; int Integer::static_int=0; void Integer::Fun(int num1,int num2,Integer *ptr) { //member_int=num1; 错误,不知对哪个对象上的member_int进行操作 ptr->member_int=num1;//正确 static_int=num2;//正确 } void Integer::Display() { cout<<"member_int="<<member_int<<endl; cout<<"static_int="<<static_int<<endl; } int main(int argc,char *argv[]) { Integer obj; Integer::Fun(1,2,&obj);//正确,仅对静态成员函数正确 obj.Display(); obj.Fun(3,4,&obj);//正确 obj.Display(); return 0; }
C++ Primer Plus学习笔记之静态成员,布布扣,bubuko.com
原文地址:http://blog.csdn.net/cstopcoder/article/details/24809403