How does C++ Preprocessors work?

假装没事ソ 提交于 2021-01-30 09:08:48

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/38809191/how-does-c-preprocessors-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!