类里面的static和函数指针的特殊事项

 
  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. }

其实,一些很变态的使用操作都在上面了,这里仅仅是一些很少用到的东西,注意拉!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值