Why we can use ## like this?

Head File:
1
2
3
4
5
6
7
8
9
10
#define CLASS_ITEM(name,price)
class ITEM_##name
{
  public :
     ITEM_##name();
     ......
     virtual ~ITEM_##name(){};
  private:
     ......
}


CPP File:
ITEM_book::ITEM_book(Char* name, int price){};

Why we can use ## like this?
Any feedback will be appreciated.

Thanks.
~Purebel
Please don't post the same question more than once!
@purebel

You defined a macro

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Advanced Macro Tricks
In addition to simple substitution, the preprocessor can also perform a bit of extra work on macro arguments, such as turning them into strings or pasting them together.
Pasting Tokens
Each argument passed to a macro is a token, and sometimes it might be 
expedient to paste arguments together to form a new token. This could come 
in handy if you have a complicated structure and you'd like to debug your program by printing out different fields. Instead of writing out the whole 
structure each time, you might use a macro to pass in the field of the structure to print.

To paste tokens in a macro, use ## between the two things to paste together.

For instance

#define BUILD_FIELD(field) my_struct.inner_struct.union_a.##field

Now, when used with a particular field name, it will expand to something like

my_struct.inner_struct.union_a.field1

The tokens are literally pasted together.  
Last edited on
Thanks very much!
It's clear for me now.
Thanks very much!
It's clear for me now.
Topic archived. No new replies allowed.