-1
#define DECLARE_DELETE_PTR(type) \
void DeletePtr_##type(string &operand) \
{\
}\

##C++中的宏定义是什么意思?

下面的代码有什么区别?

#define MAKE_STRINGS(VAR) #VAR

它只有一个#,但前者是两个#

4

3 回答 3

3

It asks the precompiler to concatenate two tokens.

#define DECLARE_DELETE_PTR(type) \
void DeletePtr_##type(string &operand) \
{\
}\

DECLARE_DELETE_PTR(int) would give :

void DeletePtr_int(string &operand)
             //^^^ the macro argument is int, so the precompiler replaces it here
{
}

Indeed, in the macro code, the argument type is concatenated with the rest of the command. If the macro argument is int, then it's just a simple subsitution giving the above result. Remember that since it's a pre-processor directive, it happens entirely at compile time.

If you're on linux, I recommand giving a look at the cpp command to try and understand better.


As for your second question, the difference is that it's simply two different operators.

Just as the name implies -> it turns its argument into a c-string (I've just tried that). For example :

std::cout << MAKE_STRINGS(Hello World) << std::endl;

would turn into:

std::cout << "Hello World" << std::endl

Or, more interesting :

std::cout << MAKE_STRINGS("Hello" World) << std::endl;

becomes:

std::cout << "\"Hello\" World" << std::endl;

It appears it also takes care of escaping special characters, but I could be wrong - that comes from experimentation from 3 minutes ago.

于 2013-05-13T05:18:32.927 回答
2

It concatenates the value you pass through the parameter type...

DECLARE_DELETE_PTR(gremlin)

Would expand to:

void DeletePtr_gremlin(string &operand)
{
}
于 2013-05-13T05:18:52.467 回答
2

## 运算符用于连接两个标记。这是一个例子:

DECLARE_DELETE_PTR(MyType) 

// 将扩展为

void DeletePtr_MyType(string &operand)
{
}
于 2013-05-13T05:22:24.223 回答