c++ : 靜態成員函數

來源:互聯網
上載者:User

標籤:name   對象   div   out   static   函數指標   執行個體   log   建立   

1. 靜態成員函數的地址可以用普通函數指標儲存,而普通成員函數地址需要用類成員函數指標來儲存。

 1 class base{ 2     public: 3         static int func1(){}; 4         int func2(){}; 5 }; 6  7  8 int main() 9 {10     int(*pf1)()=&base::func1; //普通的函數指標11     int(base::*pf2)()=&base::func2; //成員函數指標12     return 0;13 }

2. 靜態成員函數不可以調用類的非靜態成員。因為靜態成員函數不含this指標。

3.靜態成員函數不可以同時聲明為virtual, const, volatile函數。

4. 靜態成員函數無需建立任何執行個體對象就可以訪問。

 1 #include<iostream> 2 using namespace std; 3  4 class M 5 { 6     public: 7         M(int a){ 8             A=a; 9             B+=a;}10         static void f1(M m);11     private:12         int A;13         static int B;14 };15 16 void M::f1(M m)17 {18     cout << "A=" << m.A << endl;19     cout << "B=" << B << endl;20 }21 22 int M::B=0;23 24 int main()25 {26     M P(5), Q(10);27     M::f1(P);28     M::f1(Q);29     return 0;30 }
[[email protected] test]$./a.out A=5B=15A=10B=15

 

c++ : 靜態成員函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.