Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

How does C++ Preprocessors work?

I am currently looking at open source C++ projects to learn more about C++. One of the project I am look at is located at:

https://github.com/Myzilla-Web-Resources/OpenBlox/

I understand most of the source, but something I don't understand is how he is using the Preprocessors to declare a C++ class.

For instance,

static_init.h

#define DECLARE_STATIC_INIT(ClassName) 
static void static_init_func(); 
static OpenBlox::static_init_helper ClassName##_static_init_helper

namespace OpenBlox{
class static_init_helper{
    public:
        static_init_helper(init_func_type f){
            static_init::instance().add_init_func(f);
        }
};

Instance.h

#define DECLARE_CLASS(Class_Name) 
    virtual Instance* cloneImpl(); 
    virtual QString getClassName(); 
    virtual int wrap_lua(lua_State* L); 
    DECLARE_STATIC_INIT(Class_Name); 
    protected: 
        static QString ClassName; 
        static QString LuaClassName

Frame.h

namespace ob_instance{
class Frame: public GuiObject{
    public:
        Frame();
        virtual ~Frame();

        virtual void render();

        DECLARE_CLASS(Frame);
};
}
#endif

Please note that Frame.h inherited all of Instance.h functions/processors. Could someone explain to me how this work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The preprocessor is just a text find and replace, so in the Frame definition you showed, the preprocessor first sees DECLARE_CLASS(Frame) and replaces it with the content of the DECLARE_CLASS macro, becoming

namespace ob_instance{
class Frame: public GuiObject{
    public:
        Frame();
        virtual ~Frame();

        virtual void render();

        virtual Instance* cloneImpl();
        virtual QString getClassName();
        virtual int wrap_lua(lua_State* L);
        DECLARE_STATIC_INIT(Frame);
    protected:
        static QString ClassName;
        static QString LuaClassName;
};
}

(I cleaned up the formatting, in reality the entire replacement text is on one line).

It then backs up to just before the text it inserted, starts reading through again, and sees DECLARE_STATIC_INIT(Frame) and replaces that:

namespace ob_instance{
class Frame: public GuiObject{
    public:
        Frame();
        virtual ~Frame();

        virtual void render();

        virtual Instance* cloneImpl();
        virtual QString getClassName();
        virtual int wrap_lua(lua_State* L);
        static void static_init_func();
        static OpenBlox::static_init_helper Frame_static_init_helper;
    protected:
        static QString ClassName;
        static QString LuaClassName;
};
}

(The ## is the token concatenation operator)

Giving you the final Frame class definition.

As mentioned by Chris Beck in the comments, you can use the -E flag to gcc or clang to have the compiler output the preprocessed file instead of compiling it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...