代替C++虛函數

來源:互聯網
上載者:User
/**//*----------------------------------------------------------------------------------------------------
    DESCRIPTION :
        The following code is used to instead C++ virtual function mechanism for efficiency.
        "DECLARE_TYPE" is to be put in base class definitions, the "type" here is used to
        determine the type of derived classes.
        "VIRTUAL" is supposed to instead the "virtual" keyword so that the mechanism presented
        here can work properly without too many changes in the source code.
        "VOID_VIRTUAL_BEGIN" is used to define a virtual function in base classes, the
        "_funcname" must be written with full type, scope and parameter list. What's more,
        a "FUNCCALL" macro must be defined as a call to the derived classes' functions in the
        right form, and after declaring the virtual function, you can #undef it to keep clean.
        "VOID_DERIVED_CLASS" should be placed below the implementation of the base classes'
        virtual functions, and all the derived classes must be included even the derived
        classes of its directly derived classes and their inherited classes and etc, the
        "_class" here is the name of its derived class, and "_enum" is the idiographic value
        of "type" as mentioned above. "VOID_VIRTUAL_END" ends the function definition.
        similarly, "VIRTUAL_BEGIN", "DERIVED_CLASS" and "VIRTUAL_END" are used to define
        a virtual function in base classses but with value return.
        When debugging, the code will use standard C++ virtual function mechanism for ease.
        Spread it free. Len3d. Jan 15, 2006.
----------------------------------------------------------------------------------------------------*/
#define DECLARE_TYPE(_datatype)                    _datatype    type;
#ifdef _DEBUG
#define VIRTUAL                                    virtual
#define VOID_VIRTUAL_BEGIN(_funcname)            void _funcname {
#define VOID_DERIVED_CLASS(_class,_enum)
#define VOID_VIRTUAL_END                        }
#define VIRTUAL_BEGIN(_funcdef)                    _funcdef {
#define DERIVED_CLASS(_class,_enum)
#define VIRTUAL_END                                }
#else
#define VIRTUAL                                    inline
#define VOID_VIRTUAL_BEGIN(_funcname)            void _funcname { switch(type) { default:
#define VOID_DERIVED_CLASS(_class,_enum)        break; case _enum: ((_class*)this)->##FUNCCALL;
#define VOID_VIRTUAL_END                        break; } }
#define VIRTUAL_BEGIN(_funcdef)                    _funcdef { switch(type) { default:
#define DERIVED_CLASS(_class,_enum)                break; case _enum: return ((_class*)this)->##FUNCCALL;
#define VIRTUAL_END                                } }
#endif
/**//*----------------------------------------------------------------------------------------------------
    EXAMPLE :
class MyPeople {
public:
    MyPeople() {    type = 0;    }
    virtual ~MyPeople() {}
    VIRTUAL    int    func(int);
public:
    DECLARE_TYPE( int )
};
class MyMan : public MyPeople {
public:
    MyMan() {    type = 1;    }
    virtual ~MyMan() {}
    VIRTUAL int    func(int);
};
class MyWorker : public MyMan {
public:
    MyWorker() {    type = 2;    }
    virtual ~MyWorker() {}
    VIRTUAL int    func(int);
};
#define FUNCCALL    func(i)
VIRTUAL_BEGIN( int MyPeople::func(int i) )
    return i + 3;
    DERIVED_CLASS( MyMan, 1 )
    DERIVED_CLASS( MyWorker, 2 )
VIRTUAL_END
#undef FUNCCALL
#define FUNCCALL    func(i)
VIRTUAL_BEGIN( int MyMan::func(int i) )
    return i - 3;
    DERIVED_CLASS( MyWorker, 2 )
VIRTUAL_END
#undef FUNCCALL
int MyWorker::func(int i) {
    return i * 3;
}
int main(int argc, _TCHAR* argv[]) {
    MyPeople    *p1 = new MyPeople;
    MyPeople    *p2 = new MyMan;
    MyPeople    *p3 = new MyWorker;
    MyMan        *p4 = new MyWorker;
    printf( "%d\n", p1->func(5) );
    printf( "%d\n", p2->func(5) );
    printf( "%d\n", p3->func(5) );
    printf( "%d\n", p4->func(5) );
    getch();
    delete p1;
    delete p2;
    delete p3;
    delete p4;
    return 0;
}
----------------------------------------------------------------------------------------------------*/

聯繫我們

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