[Reprinted] uses macro definitions to describe C and C ++ code

Source: Internet
Author: User

InVimer's App World [http://www.vimer.cn] saw thisArticle, Have a deeper understanding of macro, so reprinted for later learning.

 

================ The following is the reprinted content ====================

I encountered a problem at work today and felt very representative, Abstract:
From the perspective of the design pattern, it is the template method. There is already a base class, and many subclasses need to be defined to implement its method.
However, only some of the class names are different, and only some of the input parameters of the constructor are different.
For exampleCode:
# 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 ;
}
The output is as follows:
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
This is a very disgusting code, and it is very easy to write errors. If it is Python, because of its own introspection ability, it will not have such a problem, but C ++ does not have this ability, so we can only send Hope and macro definition.
Let's take a look at our rewritten code:
# 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 ;
}
The input result is:
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
OK, solve the problem!

There may be multiple solutions to the problem itself in different scenarios, but this at least provides a different perspective for the problem, hoping to be useful to everyone.  


This articleThe world of vimer [http://www.vimer.cn] 

source URL: http://www.vimer.cn/2011/01/%e5%b7%a7%e7%94%a8%e5% AE %8f%e5% AE %9a%e4%b9%89%e6%9d%a5%e7% AE %80%e5%86%99%e4%bb%a3%e7%a0%81.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.