|
-
February 27th, 2008, 12:24 PM
#1
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.
-
February 27th, 2008, 12:43 PM
#2
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::*)()>.
-
February 27th, 2008, 01:54 PM
#3
Re: Token pasting
 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
-
February 27th, 2008, 02:07 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|