- #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;
- }
其实,一些很变态的使用操作都在上面了,这里仅仅是一些很少用到的东西,注意拉!