1

Imagine I have a template class, where I've defined typdedef of function pointer to BaseClass function, that returns T and has no arguments:

template <typename T, typename BaseClass> class TConnectionPoint
{
    typedef   T (BaseClass::* PConnectionFunction)();
    private:
       PConnectionFunction _func;
    public:
       T Call();
}

This template has method Call():

template <typename T, typename BaseClass> T TConnectionPoint<T,BaseClass>::Call()
{
    return _func();
}

The problem appears when I try to execute Call() function - VS2013 send me this error, which is a little confusing me:

term does not evaluate to a function taking 0 arguments

What I'm doing wrong?

Jimmy_st
  • 61
  • 1
  • 10

2 Answers2

1

A pointer to member function can only be dereferenced in combination with an object (on which the function will be invoked). You need access to an object of type BaseClass to use it.

For example, you could change Call() like this:

template <typename T, typename BaseClass>
T TConnectionPoint<T,BaseClass>::Call(BaseClass &obj)
{
    return (obj.*_func)();
}
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
1

The typedef defines PConnectionFunction to be a member function pointer, not a plain function pointer. You can call member functions only on an object, like this:

template <typename T, typename BaseClass> 
T TConnectionPoint<T,BaseClass>::Call(BaseClass& b)
{
    return (b.*_func)();
}

If instead you want to call static member functions ob BaseClass, then the function pointer needs to be a normal function pointer, not a member function pointer:

typedef   T (* PConnectionFunction)();
Arne Mertz
  • 24,171
  • 3
  • 51
  • 90