首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

怎样把宏改为类或者函数?解决方法

2012-02-25 
怎样把宏改为类或者函数?请问哪里有这样的例子,把宏改为类或者函数的?现在有一段老的代码,想改写为类,请大

怎样把宏改为类或者函数?
请问哪里有这样的例子,把宏改为类或者函数的?   现在有一段老的代码,想改写为类,请大家指点,   在线等!非常感谢!!

#define   START_EXPLAIN                                                                                           \
    private:                                                                                                                 \
    static   void   explain_macro()                       \
        {                                                                                                                                      


////////////////////////////////////////////////////////////////////////
#define   EXPLAIN(line){                                                         \
        std::string   anStr(line);\
        CPPUNIT::GlobalListener::instance()-> addInfo(   "EXPLAIN ",   anStr.c_str());   \
        }
////////////////////////////////////////////////////////////////////////
#define   END_EXPLAIN                                                                                                   \
        }                                                                                                                               \
    public:  

////////////////////////////////////////////////////////////////////////
#define   START_TEST(method)                                                                                       \
    private:                                                                                                                       \


    static   void   method##_explain()                             \
        {                                                                                                                                 \
       
////////////////////////////////////////////////////////////////////////
#define   TEST(line){                                                                                                     \
        std::string   anStr(line);                                                                 \
        CPPUNIT::GlobalListener::instance()-> addInfo(   "TEST ",   anStr.c_str());   \
        }

////////////////////////////////////////////////////////////////////////
#define   END_TEST                                                                                                           \
        }                                                                                                                                 \
    public:

[解决办法]
没这个必要吧

改成函数的话应该不难
去掉#define 加上函数返回值,加上参数类型
宏跟函数结构上差不多
[解决办法]
#define EXPLAIN(line){ \
std::string anStr(line);\
CPPUNIT::GlobalListener::instance()-> addInfo( "EXPLAIN ", anStr.c_str()); \
}

==>
void EXPLAIN(char *line){
std::string anStr(line);
CPPUNIT::GlobalListener::instance()-> addInfo( "EXPLAIN ", anStr.c_str());
}

宏的可以把#define全部换成void
然后在括号中加上参数类型, 去掉\
既是

[解决办法]
#define START_TEST(method) \
private: \
static void method##_explain() \
{



这个不是函数

这个是为在类定义的时候展开用的, 这个如果改成函数不用管

#define TEST(line){ \
std::string anStr(line); \
CPPUNIT::GlobalListener::instance()-> addInfo( "TEST ", anStr.c_str()); \
}
可以改成函数
[解决办法]
类,不是把东西凑在一起就是类的,楼主不要乱用类

热点排行
Bad Request.