zoukankan      html  css  js  c++  java
  • 代替C++虚函数

    /*----------------------------------------------------------------------------------------------------
        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;
    }
    ----------------------------------------------------------------------------------------------------
    */
  • 相关阅读:
    [leetcode]34.Find First and Last Position of Element in Sorted Array找区间
    [leetcode]278. First Bad Version首个坏版本
    [leetcode]367. Valid Perfect Square验证完全平方数
    [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)
    [leetcode]55. Jump Game青蛙跳(能否跳到终点)
    [leetcode]26. Remove Duplicates from Sorted Array有序数组去重(单个元素只出现一次)
    [leetcode]27. Remove Element删除元素
    [leetcode]20. Valid Parentheses有效括号序列
    [leetcode]15. 3Sum三数之和
    C#中的局部类型
  • 原文地址:https://www.cnblogs.com/len3d/p/323833.html
Copyright © 2011-2022 走看看