What does # and \ represent in C++ ?
I came across a piece of code which is a RTTI class. (run time type information). The class is called "RTTI".
There are a few defines below the class declaration and one of them is this:
#define RTTI_IMPL(name,parent) \
const RTTI name::rtti(#name,parent::rtti);
rtti is defined as "static const RTTI rtti;"
An example of how the #define is used is this:
RTTI_IMPL(BillBoard,GameObject); where BillBoard is the child class and GameObject is the parent class of Billboard.
What does '\' mean in the #define statement and also, what does # in "#name" represent?
On another note, I came across a method declaration like this:
virtual void RegisterEvents(class EventHandlerComponent *pkEventHandler);
EventHandlerComponent is a class which is declared and defined in another .h and cpp file. What does adding the keyword "class" in the method's parameter do? Is there a difference if I use this instead?
virtual void RegisterEvents(EventHandlerComponent *pkEventHandler);
Re: What does # and \ represent in C++ ?
Quote:
Originally Posted by ZhiYi
What does '\' mean in the #define statement and also, what does # in "#name" represent?
The backslash escapes the new line, so to speak, allowing the macro definition to extend to the next line. The #name means that the argument corresponding to name is transformed to a string, e.g.,
Code:
RTTI_IMPL(BillBoard,GameObject);
becomes:
Code:
const RTTI BillBoard::rtti("BillBoard",GameObject::rtti);
Quote:
Originally Posted by ZhiYi
What does adding the keyword "class" in the method's parameter do?
Interesting: I have not seen this used, but I guessed that it was meant to correspond to the legacy use of the struct keyword in a struct name in C. In C, if you just define a struct X without a typedef to X, you would need to use struct X as the struct name. In C++, you just need to use X even without such a typedef, but can redundantly use struct X if you want. It appears (and is confirmed by my compiler) that the class keyword can be used redundantly in the same way.
Re: What does # and \ represent in C++ ?
thanks for the explanations! :o
Re: What does # and \ represent in C++ ?
Quote:
Interesting: I have not seen this used, but I guessed that it was meant to correspond to the legacy use of the struct keyword in a struct name in C.
I'm not 100% sure, but I think it can act as sort of an "inline forward declaration" (for lack of a better term). Eg, both Comeau and MSVC9 accept the following code:
Code:
void foo(const class Bar&) {}
class Bar {};
int main()
{
Bar b;
foo(b);
return 0;
}
Re: What does # and \ represent in C++ ?
Quote:
Originally Posted by Speedo
I'm not 100% sure, but I think it can act as sort of an "inline forward declaration" (for lack of a better term).
That would also be consistent with C.