This is what I try to achieve:

class MyClass
{
    public:
    template<typename T>
    void whenEntering( const std::string& strState, 
                       T& t, 
                       void T::(*pMemberFunction)(void)) /// compilation fails here
    {
        t.(*pMemberFunction)(); // this line is only an example
    }
}

It's for a kind of callback system for reacting to some events I receive.

However Visual 2010 gives me the following compilation error:

    error C2589: '(' : illegal token on right side of '::'

I may be wrong with the pointer-to-member syntax... but I am also afraid that I may not define a template this way... do you have any idea ?

有帮助吗?

解决方案

You want void (T::*pMemberFunction)(void)

The other issue is probably just a typo in your example usage, but calling the member function uses .* as a single operator; you can't have a ( in-between them, or even whitespace. I'm guessing that's a typo because it's almost the correct way to deal with the weird operator precedence that pointer-to-member operators have:

(t.*pMemberFunction)();

其他提示

There are several problems in your code. In particular, the syntax for declaring a pointer to member function is void (T::* pMemberFunction)(void):

Overall, this is how your code should look like:

class MyClass
{
    public:
    template<typename T>
    void whenEntering( const std::string& strState,
                       T& t,
                       void (T::* pMemberFunction)(void)
                               ) /// this fails
    {
        t.*pMemberFunction(); // this line is only an example
    }
};
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top