辅导:类里面的static和函数指针的特殊事项
来源:优易学  2011-11-7 11:14:57   【优易学:中国教育考试门户网】   资料下载   IT书店
  #include <iostream>
  using namespace std;
  int print() {
  cout << "YYYYYYYYY\n";
  }
  class tt{
  public:
  static int (*pp)();
  static int *p;
  int v() {
  this->pp(); //static can be ivoked by this ,but ......
  }
  static int vv() {
  // this -> pp(); No , only static member or func can be evoked!
  }
  };//青年人网站编注
  int (*tt::pp)() = print; //like this to initialise!
  int *tt::p = NULL; //must 1. * 2. tt:: and 3. p
  int main() {
  tt a;
  a.pp();
  (*tt::pp)();
  cout << sizeof(tt) << endl; //1
  }
  #include <iostream>
  using namespace std;
  void print() {
  cout << "asdf\n";
  }
  class tt{
  public:
  void (*p) ();
  tt() {
  p = &(::print);
  }
  int a() {
  cout << "aaaaa function!\n";
  }
  };
  typedef int (tt::*MM)() ;
  int main() {
  cout << sizeof(tt) << endl; //4 not 1, void *p in class tt is a val, not a function! so sizeof is 4
  tt a;
  a.p();
  cout << &tt::a << endl;
  //cout << (int )& a.a << endl;
  MM myf = &tt::a;
  (a.*myf)(); //like this
  }
  #include <iostream>
  using namespace std;
  class tt {
  public:
  static int a;
  };
  int tt::a = 10;
  int main() {
  cout << sizeof(tt) << endl; //1, static , no matter it is const or pointer or reference! save in static section, not class
  //scope!
  cout << tt::a << endl;
  }
  #include <iostream>
  using namespace std;
  class tt {
  public:
  double a;
  char b;
  virtual int _a() {}
  virtual int _b() {}
  };
  int main() {
  cout << sizeof(tt) << endl; //Linux 16, Windows 24, and vptr in the beginning class!
  //but align of windows is 8,because of the double,but align of the Linux is 4,even if
  //u use #pragma pack(8), the align is still 4 BYTE!
  }
  #include <iostream>
  using namespace std;
  class T{
  public:
  char aa;
  double i;
  virtual int a() {}
  };
  int main() {
  //cout << &(((T *)1)->aa) << endl; //can use it in Windows! LInux : can't
  //get the aa's addr, to get that the vptr is
  // at the beginning or the end of the class or the class object!
  T *a = new T;
  cout << (int)&(a->aa) << endl;
  cout << (int)a << endl;
  cout << sizeof(T) << endl;
  delete a;
  }

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训