Home  >  Q&A  >  body text

有关c++宏定义的疑问?

这一部分宏定义代码看不懂,请问能解读一下吗?另外有哪些地方可以系统的学习这些宏定义?好多C++的书上都没有讲。

天蓬老师天蓬老师2764 days ago531

reply all(3)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 13:59:11

    In fact, the name of this macro has clearly stated its function to a large extent: automatically register a module with GameServerModuleMgr when the program starts. This actually implements a static plug-in system.

    It is not difficult to guess that the usage of this macro should be similar to the following:

    class MyModuleA : public AbstractModule {
    blah...
    };
    REG_GAMESERVER_MODULE(MyModuleA);

    The system has a globally unique GameServerModuleMgr singleton, and we can obtain its instance through GameServerModuleMgr::getInstance(). The so-called registration module is to call the regModule of the class and pass in the instance of the class to be registered. That is:

    GameServerModuleMgr::getInstance()->regModule(new MyModuleA());

    The remaining problem is that we usually don’t want to manually register each of our Modules with GameServerModuleMgr in the main function (or in our initialization code), so we need an automatic way. Considering that in C++, the constructor of the global object is automatically executed by the CRT before main is executed, so we can construct the following class:

    struct REG_MyModuleA {
        REG_MyModuleA() {
            GameServerModuleMgr::getInstance()->regModule(new MyModuleA());
        }
    };
    
    # 创建全局对象,最终导致MyModuleA被自动注册(通过其构造函数)
    # 声明为static避免该对象被其他代码访问到 (Translation Unit Private)
    static REG_MyModuleA sg_ REG_MyModuleA;

    REG_MyModuleA has no functional significance to our program. Its function is only to enable the above automatic registration process to occur. What that macro does is nothing more than replace MyModuleA here with a variable macro parameter, thereby providing a handy helper to Module authors. Regarding the specific grammar (##sticky symbols, etc.), the above students have already made it very clear, so I won’t go into details here.

    reply
    0
  • PHPz

    PHPz2017-04-17 13:59:11

    The function of this macro: declare a new struct every time the macro is used, and declare a static object of this type.
    For example REG_GAMESERVER_MODULE(ABC) is equivalent to:

    struct REG_ABC
    {
        REG_ABC(){
            //some stuff
        }
    };
    static REG_ABC object;
    

    Benefits: Less typing, less effort in adding new modules of the same type
    Disadvantages: There are many shortcomings that I won’t list, and search engines can find a lot of them.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:59:11

    My understanding is:

    1. Macro definition is a direct replacement of characters

    2.# and ## are used for stringification of parameters

    Give me an example:

    #define TEST(arg)  int test_##arg; 
    #define TEST2(arg) int #arg;

    Call:

    TEST(val); -> int test_val;
    TEST2(val);-> int val;
    

    # is used to get the parameter character value, ## is used to connect the parameter character value

    So call in code:

    REG_GAMESERVER_MODULE(cls);
    
    Replace

    with:

       struct REG_cls;
       ...
       static REG_cls sg_REG_cls;

    Then look at the replaced code. It should declare a Struct REG_cls based on the parameter cls, and then implement its constructor. Finally declare a corresponding static variable.

    reply
    0
  • Cancelreply