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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/11/30 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 类中的静态成员函数访问非静态成员变量 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

1.思路:

靜態(tài)成員函數(shù)屬于類(lèi)(通過(guò)類(lèi)訪問(wèn),調(diào)用函數(shù)時(shí)沒(méi)有提供this指針),
非靜態(tài)成員函數(shù)屬于實(shí)例(通過(guò)對(duì)象訪問(wèn))(默認(rèn)都提供了this指針),
非靜態(tài)成員也屬于實(shí)例(通過(guò)對(duì)象訪問(wèn)),
所以,要想在靜態(tài)成員函數(shù)訪問(wèn)非靜態(tài)成員變量,
無(wú)非就是實(shí)例化一個(gè)對(duì)象,然后通過(guò)對(duì)象訪問(wèn)非靜態(tài)成員變量。



2.實(shí)現(xiàn)方法

第一種:類(lèi)外實(shí)例化對(duì)象,傳參數(shù)(看自己需要使用值、地址、引用)

[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;??
  • ???}??







  • 第二種:類(lèi)外實(shí)例化對(duì)象,全局對(duì)象

    [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;??
  • ?????}??








  • 第三種:類(lèi)中實(shí)例化對(duì)象,在創(chuàng)建的時(shí)候把this指針賦值給那個(gè)靜態(tài)成員

    [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;????????????????????????//靜態(tài)成員需要初始化??
  • ??
  • ??
  • ????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;??
  • ???}??



  • 單實(shí)例



    [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;????????????????????????//靜態(tài)成員需要初始化????
  • ????
  • ????
  • ????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;????
  • ???} ? ?

  • 總結(jié)

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

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。