巧用宏定义来简写C,C++代码

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
using namespace std;
class EMA
{
    public:
        EMA(string a,string b,string c,string d,string e)
        {
            cout <<a<<","<<b<<","<<c<<","<<d<<","<<e<<endl;
        }
};
class EMA4CGI_1ST:public EMA{
    public:
        EMA4CGI_1ST():EMA(
                "app_mng.1ST_EMA_AVG",
                "app_mng.1ST_EMA_HWM",
                "app_mng.1ST_EMA_TMO",
                "app_mng.1ST_EMA_N",
                "app_mng.1ST_EMA_RATIO"
                ){}
        ~EMA4CGI_1ST() {}
};
class EMA4CGI_2ND:public EMA{
    public:
        EMA4CGI_2ND():EMA(
                "app_mng.2ND_EMA_AVG",
                "app_mng.2ND_EMA_HWM",
                "app_mng.2ND_EMA_TMO",
                "app_mng.2ND_EMA_N",
                "app_mng.2ND_EMA_RATIO"
                ){}
        ~EMA4CGI_2ND() {}
};
class EMA4CGI_3RD:public EMA{
    public:
        EMA4CGI_3RD():EMA(
                "app_mng.3RD_EMA_AVG",
                "app_mng.3RD_EMA_HWM",
                "app_mng.3RD_EMA_TMO",
                "app_mng.3RD_EMA_N",
                "app_mng.3RD_EMA_RATIO"
                ){}
        ~EMA4CGI_3RD() {}
};
int main(int argc, const char *argv[])
{
    EMA4CGI_1ST();
    EMA4CGI_2ND();
    EMA4CGI_3RD();
    return 0;
}

输出如下:

app_mng.1ST_EMA_AVG,app_mng.1ST_EMA_HWM,app_mng.1ST_EMA_TMO,app_mng.1ST_EMA_N,app_mng.1ST_EMA_RATIO
app_mng.2ND_EMA_AVG,app_mng.2ND_EMA_HWM,app_mng.2ND_EMA_TMO,app_mng.2ND_EMA_N,app_mng.2ND_EMA_RATIO
app_mng.3RD_EMA_AVG,app_mng.3RD_EMA_HWM,app_mng.3RD_EMA_TMO,app_mng.3RD_EMA_N,app_mng.3RD_EMA_RATIO

非常恶心的代码,而且非常容易写错,如果是python,由于其本身自省的能力,所以不会存在这样的问题,但是C++可没有这种能力,所以我们只能寄希望与宏定义了。
直接来看一下我们改写的代码:

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
using namespace std;
class EMA
{
    public:
        EMA(string a,string b,string c,string d,string e)
        {
            cout <<a<<","<<b<<","<<c<<","<<d<<","<<e<<endl;
        }
};
#define EMACLASS_DEFINITION(class_name)\
    class EMA4CGI_##class_name:public EMA{\
        public:\
            EMA4CGI_##class_name():EMA(\
                    "app_mng."#class_name"_EMA_AVG",\
                    "app_mng."#class_name"_EMA_HWM",\
                    "app_mng."#class_name"_EMA_TMO",\
                    "app_mng."#class_name"_EMA_N",\
                    "app_mng."#class_name"_EMA_RATIO"\
                    ){}\
            ~EMA4CGI_##class_name() {}\
    };

EMACLASS_DEFINITION(1ST)
EMACLASS_DEFINITION(2ND)
EMACLASS_DEFINITION(3RD)

int main(int argc, const char *argv[])
{
    EMA4CGI_1ST();
    EMA4CGI_2ND();
    EMA4CGI_3RD();
    return 0;
}

输出结果:

app_mng.1ST_EMA_AVG,app_mng.1ST_EMA_HWM,app_mng.1ST_EMA_TMO,app_mng.1ST_EMA_N,app_mng.1ST_EMA_RATIO
app_mng.2ND_EMA_AVG,app_mng.2ND_EMA_HWM,app_mng.2ND_EMA_TMO,app_mng.2ND_EMA_N,app_mng.2ND_EMA_RATIO
app_mng.3RD_EMA_AVG,app_mng.3RD_EMA_HWM,app_mng.3RD_EMA_TMO,app_mng.3RD_EMA_N,app_mng.3RD_EMA_RATIO

你可能感兴趣的:(宏)