CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2005
    Posts
    200

    Red face 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);

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 2005
    Posts
    200

    Re: What does # and \ represent in C++ ?

    thanks for the explanations!

  4. #4
    Join Date
    Aug 2007
    Posts
    858

    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;
    }

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured