How does C++ Preprocessors work?

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.

Leave a Comment