这个宏功能意味着什么?

时间:2014-01-10 19:13:11

标签: c++ c macros

#define CONCAT(a,b) a##b
#define METHOD(method)    \
public:    \
static int CONCAT(method,wrapper)(int* ptri, char* ptrc, double* ptrd)    \
{    \
  return ((type *)ptri)->method(ptrc,ptrd);    \
}

问题1:这个方法是类似于类还是结构?

问题2:对于

static int CONCAT(method,wrapper)(int* ptri, char* ptrc, double* ptrd)

是不是意味着:

static int methodwrapper(int* ptri, char* ptrc, double* ptrd)

3 个答案:

答案 0 :(得分:3)

这不是那样的,它只是一种混淆代码的方式(或者以懒惰的方式一遍又一遍地写同样的东西)

以下

class X
{

    public: 
    static int SomeMethodwrapper(int * ptri, char* ptrc, double* ptrd)    
    {    
      return ((type *)ptri)->SomeMethod(ptrc,ptrd);    
    }
    public:    
    static int SomeOtherMethodwrapper(int * ptri, char* ptrc, double* ptrd)    
    {    
      return ((type *)ptri)->SomeOtherMethod(ptrc,ptrd);    
    }

};

因此可以写成

class X
{
    METHOD(SomeMethod)
    METHOD(SomeOtherMethod)
};

答案 1 :(得分:0)

从宏的名称开始,它只是连接两个标记,例如

CONCAT( class, Method )

结果

classMethod

对于宏#define METHOD(方法),它定义了一个静态公共方法,并使用宏CONCAT来形成方法的名称。

答案 2 :(得分:0)

我认为Luchian Grigore有一个完美而准确的答案。但是,如果我有机会,我会重构代码,使其更清晰,更具可读性。我知道有些编写者只是为了让事情变得复杂而写作,因为他们认为他们是极客或其他什么。看一下这篇文章的更多例子,非常有用:http://www.codeproject.com/Articles/25541/C-C-macros-programming

我还建议你自己尝试一些例子。这将给你一个深刻的理解。这是学习编程的一种很好的态度。