類里面的static和函數(shù)指針的特殊事項(xiàng)

字號(hào):

#include
    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
    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
    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
    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
    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;
    }