類裡面的static和函數指標的特殊事項

來源:互聯網
上載者:User
 
  1. #include <iostream>
  2. using namespace std;
  3. int print() {
  4.     cout << "YYYYYYYYY/n";
  5. }
  6. class tt{
  7.     public:
  8.         static int (*pp)();
  9.         static int *p;
  10.         int v() {
  11.             this->pp(); //static can be ivoked by this ,but ......
  12.         }       
  13.         static int vv() {
  14.         //  this -> pp(); No , only static member or func can be evoked!
  15.         }
  16. };
  17. int (*tt::pp)() = print; //like this to initialise!
  18. int *tt::p = NULL;       //must 1. * 2. tt:: and 3. p
  19. int main() {
  20.     tt a;
  21.     a.pp();
  22.     (*tt::pp)();
  23.     cout << sizeof(tt) << endl; //1
  24. }
  25. #include <iostream>
  26. using namespace std;
  27. void print() {
  28.     cout <<  "asdf/n";
  29. }
  30. class tt{
  31.     public:
  32.         void (*p) ();
  33.         tt() {
  34.             p = &(::print);
  35.         }
  36.         int a() {
  37.             cout << "aaaaa function!/n";
  38.         }
  39. };
  40. typedef int (tt::*MM)() ;
  41. int main() {
  42.     cout << sizeof(tt) << endl; //4 not 1, void *p in class tt is a val, not a function! so sizeof is 4
  43.     tt a;
  44.     a.p();
  45.     cout << &tt::a << endl;
  46.     //cout << (int )& a.a << endl;
  47.     MM myf = &tt::a;
  48.     (a.*myf)(); //like this
  49. }
  50. #include <iostream>
  51. using namespace std;
  52. class tt {
  53.     public:
  54.         static int a;
  55. };
  56. int tt::a = 10;
  57. int main() {
  58.     cout << sizeof(tt) << endl; //1, static , no matter it is const or pointer or reference! save in static section, not class
  59.    //scope! 
  60.     cout << tt::a << endl;
  61. }
  62. #include <iostream>
  63. using namespace std;
  64. class tt {
  65.     public:
  66.         double a;
  67.         char b;
  68.         virtual int _a() {}
  69.         virtual int _b() {}
  70. };
  71. int main() {
  72.     cout << sizeof(tt) << endl; //Linux 16, Windows 24, and vptr in the beginning class!
  73.     //but align of windows is 8,because of the double,but align of the Linux is 4,even if 
  74.     //u use #pragma pack(8), the align is still 4 BYTE!
  75. }
  76. #include <iostream>
  77. using namespace std;
  78. class T{
  79.     public:
  80.         char aa;
  81.         double i;
  82.         virtual int a() {}
  83. };
  84. int main() {
  85.     //cout << &(((T *)1)->aa) << endl; //can use it in Windows! LInux : can't
  86.     //get the aa's addr, to get that the vptr is 
  87.     //  at the beginning or the end of the class or the class object!
  88.     T *a = new T;
  89.     cout << (int)&(a->aa) << endl;
  90.     cout << (int)a << endl;
  91.     cout << sizeof(T) << endl;
  92.     delete a;
  93. }

其實,一些很變態的使用操作都在上面了,這裡僅僅是一些很少用到的東西,注意拉!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.