CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Token pasting

  1. #1
    Join Date
    Feb 2008
    Posts
    22

    Token pasting

    Hi,
    How can I use token pasting preprocessor directive? I need to call the right function given the function names in the form of an array.

    Code:
    #define CallMyFun( p, n )  p->##n ();
    
    string myFuncs[ 3 ] = { string( "Func1" ), string( "Func2" ), string( "Func3" ) };
    
    class CMyClass
    {
     public:
       bool Func1();
       bool Func2();
       bool Func3();
    };
    And then use it something like:

    Code:
    CMyClass theObject;
    CallMyFun( &theObject, myFuncs[ 0 ].c_str() );
    You can see the problem with the above code. How can I resolve the issue?
    Thanks
    SB
    PS: Is there is another way of resolving the issue via function pointers. Note that the functions are not static members.

  2. #2
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Token pasting

    Yes, it's possible. Read about pointers to member functions.

    A simple solution would be to create an std::map <std::string, bool(CMyClass::*)()>.

  3. #3
    Join Date
    Feb 2008
    Posts
    22

    Re: Token pasting

    Quote Originally Posted by Plasmator
    Yes, it's possible. Read about pointers to member functions.

    A simple solution would be to create an std::map <std::string, bool(CMyClass::*)()>.

    What about token pasting?
    -SB

  4. #4
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Token pasting

    Think about what the compiler sees after pre-processing takes place.

    When you type:
    Code:
    CMyClass theObject;
    CallMyFun( &theObject, myFuncs[ 0 ].c_str() );
    The compiler sees:
    Code:
    CMyClass theObject;
    &theObject->myFuncs[ 0 ].c_str() ();
    How do you plan on converting strings to actual symbol names?

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