-
August 24th, 2009, 11:29 AM
#1
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);
-
August 24th, 2009, 12:25 PM
#2
Re: What does # and \ represent in C++ ?
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);
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.
-
August 25th, 2009, 06:24 AM
#3
Re: What does # and \ represent in C++ ?
thanks for the explanations!
-
August 25th, 2009, 08:36 PM
#4
Re: What does # and \ represent in C++ ?
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;
}
-
August 26th, 2009, 12:48 AM
#5
Re: What does # and \ represent in C++ ?
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|