日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/11/30 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 类中的静态成员函数访问非静态成员变量 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

1.思路:

靜態成員函數屬于類(通過類訪問,調用函數時沒有提供this指針),
非靜態成員函數屬于實例(通過對象訪問)(默認都提供了this指針),
非靜態成員也屬于實例(通過對象訪問),
所以,要想在靜態成員函數訪問非靜態成員變量,
無非就是實例化一個對象,然后通過對象訪問非靜態成員變量。



2.實現方法

第一種:類外實例化對象,傳參數(看自己需要使用值、地址、引用)

[cpp]?view plain?copy
  • class?Test??
  • {??
  • public:??
  • ???????Test()?{??}??
  • ???????~Test(){??}??
  • ??????void?static?testFun(Test&?test);??
  • ??
  • private:??
  • ??????int?m_a;??
  • ??
  • ??
  • };??
  • ??
  • ??
  • ?????void?Test::testFun(Test&?test)??
  • ????{??
  • ??????????test.m_a=20;??
  • ???}??







  • 第二種:類外實例化對象,全局對象

    [cpp]?view plain?copy
  • Test?test;??
  • ??
  • ??
  • class?Test??
  • {??
  • public:??
  • ???????Test(){??}??
  • ???????~Test(){???}??
  • ???????void?static?testFun();??
  • ??
  • private:??
  • ???????int?m_a;??
  • ??
  • ??
  • };??
  • ??
  • ??
  • ?????void?Test::testFun()??
  • ????{??
  • ???????????test.m_a=20;??
  • ?????}??








  • 第三種:類中實例化對象,在創建的時候把this指針賦值給那個靜態成員

    [cpp]?view plain?copy
  • #include<stdio.h>??
  • #include<string.h>??
  • ??
  • ??
  • class?Test??
  • {??
  • public:??
  • ???????Test()?{???test?=?this?;?}??
  • ???????~Test(){??}??
  • ???????void?static?testFun();??
  • ???????void?printMember();??
  • ??
  • private:??
  • ?????int?m_a;??
  • ????static?Test*?test;??
  • ??
  • ??
  • };??
  • ???Test*?Test::test;????????????????????????//靜態成員需要初始化??
  • ??
  • ??
  • ????void?Test::testFun()??
  • ????{??
  • ??????????test->m_a=20;?????????????
  • ????}??
  • ??
  • ??
  • ?????void?Test::printMember()??
  • ????{??
  • ?????????printf("%d\n",?this->m_a);???
  • ???}??
  • ??
  • ??
  • ????int?main()??
  • ???{??
  • ????????Test?test1;??
  • ????????Test::testFun();??
  • ?????????test1.printMember();?????????????//輸出20,?成功改變成員變量m_a的值??
  • ??
  • ??
  • ?????return?0;??
  • ???}??



  • 單實例



    [cpp]?view plain?copy
  • #include<stdio.h>??
  • #include<stdlib.h>??
  • #include<time.h>??
  • #include<string.h>??
  • ??
  • ??
  • ??
  • #include<stdio.h>????
  • #include<string.h>????
  • ????
  • ????
  • class?Test????
  • {????
  • private:??
  • ????Test()?{??};??
  • public:????
  • ???????static?Test*?getTest()?{?????
  • ???????????static?Test?instance;????
  • ???????????test?=?&instance?;??
  • ???????????return?&instance;}????
  • ???????~Test(){?printf("sdg\n");?}????
  • ???????void?static?testFun();????
  • ???????void?printMember();????
  • ????
  • private:????
  • ?????int?m_a;????
  • ????static?Test*?test;????
  • ????
  • ????
  • };????
  • Test*?Test::test;????????????????????????//靜態成員需要初始化????
  • ????
  • ????
  • ????void?Test::testFun()????
  • ????{????
  • ??????????test->m_a=20;???????????????
  • ????}????
  • ????
  • ????
  • ?????void?Test::printMember()????
  • ????{????
  • ?????????printf("%d\n",?this->m_a);?????
  • ???}????
  • ????
  • ????
  • ????int?main()????
  • ???{????
  • ????????Test*?test1?=?Test::getTest();????
  • ????????Test*?test2?=?Test::getTest();????
  • ????????Test::testFun();????
  • ????????test1->printMember();?????????????//輸出20,?成功改變成員變量m_a的值????
  • ????test2->printMember();?????????????//輸出20,?成功改變成員變量m_a的值????
  • ????
  • ????
  • ?????return?0;????
  • ???} ? ?

  • 總結

    以上是生活随笔為你收集整理的类中的静态成员函数访问非静态成员变量的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。