类中的静态成员函数访问非静态成员变量

http://blog.csdn.net/u011857683/article/details/52294353

1.思路:

静态成员函数属于类(通过类访问,调用函数时没有提供this指针),
非静态成员函数属于实例(通过对象访问)(默认都提供了this指针),
非静态成员也属于实例(通过对象访问),
所以,要想在静态成员函数访问非静态成员变量,
无非就是实例化一个对象,然后通过对象访问非静态成员变量。



2.实现方法

第一种:类外实例化对象,传参数(看自己需要使用值、地址、引用)

[cpp]  view plain  copy
  1. class Test  
  2. {  
  3. public:  
  4.        Test() {  }  
  5.        ~Test(){  }  
  6.       void static testFun(Test& test);  
  7.   
  8. private:  
  9.       int m_a;  
  10.   
  11.   
  12. };  
  13.   
  14.   
  15.      void Test::testFun(Test& test)  
  16.     {  
  17.           test.m_a=20;  
  18.    }  







第二种:类外实例化对象,全局对象

[cpp]  view plain  copy
  1. Test test;  
  2.   
  3.   
  4. class Test  
  5. {  
  6. public:  
  7.        Test(){  }  
  8.        ~Test(){   }  
  9.        void static testFun();  
  10.   
  11. private:  
  12.        int m_a;  
  13.   
  14.   
  15. };  
  16.   
  17.   
  18.      void Test::testFun()  
  19.     {  
  20.            test.m_a=20;  
  21.      }  








第三种:类中实例化对象,在创建的时候把this指针赋值给那个静态成员

[cpp]  view plain  copy
  1. #include<stdio.h>  
  2. #include<string.h>  
  3.   
  4.   
  5. class Test  
  6. {  
  7. public:  
  8.        Test() {   test = this ; }  
  9.        ~Test(){  }  
  10.        void static testFun();  
  11.        void printMember();  
  12.   
  13. private:  
  14.      int m_a;  
  15.     static Test* test;  
  16.   
  17.   
  18. };  
  19.    Test* Test::test;                        //静态成员需要初始化  
  20.   
  21.   
  22.     void Test::testFun()  
  23.     {  
  24.           test->m_a=20;             
  25.     }  
  26.   
  27.   
  28.      void Test::printMember()  
  29.     {  
  30.          printf("%d\n"this->m_a);   
  31.    }  
  32.   
  33.   
  34.     int main()  
  35.    {  
  36.         Test test1;  
  37.         Test::testFun();  
  38.          test1.printMember();             //输出20, 成功改变成员变量m_a的值  
  39.   
  40.   
  41.      return 0;  
  42.    }  



单实例



[cpp]  view plain  copy
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include<time.h>  
  4. #include<string.h>  
  5.   
  6.   
  7.   
  8. #include<stdio.h>    
  9. #include<string.h>    
  10.     
  11.     
  12. class Test    
  13. {    
  14. private:  
  15.     Test() {  };  
  16. public:    
  17.        static Test* getTest() {     
  18.            static Test instance;    
  19.            test = &instance ;  
  20.            return &instance;}    
  21.        ~Test(){ printf("sdg\n"); }    
  22.        void static testFun();    
  23.        void printMember();    
  24.     
  25. private:    
  26.      int m_a;    
  27.     static Test* test;    
  28.     
  29.     
  30. };    
  31. Test* Test::test;                        //静态成员需要初始化    
  32.     
  33.     
  34.     void Test::testFun()    
  35.     {    
  36.           test->m_a=20;               
  37.     }    
  38.     
  39.     
  40.      void Test::printMember()    
  41.     {    
  42.          printf("%d\n"this->m_a);     
  43.    }    
  44.     
  45.     
  46.     int main()    
  47.    {    
  48.         Test* test1 = Test::getTest();    
  49.         Test* test2 = Test::getTest();    
  50.         Test::testFun();    
  51.         test1->printMember();             //输出20, 成功改变成员变量m_a的值    
  52.     test2->printMember();             //输出20, 成功改变成员变量m_a的值    
  53.     
  54.     
  55.      return 0;    
  56.    }    

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值