shanshanpt
shanshanpt

Reputation: 37

c++ template function pointer

I encounter a c++ template function poniter problem, please help. thanks so much.

class AAA {
  public:
    template<typename K>
    void dooo(K str) {
      std::cout << str << std::endl;
    }
};

template<typename Class, typename ret, typename K>
using Func = ret (Class::*) (K);

int main() {
  Func<AAA, void, int> myFunc = &AAA::dooo;

  myFunc(3);

  return 0;
}

Complie: clang++ -std=c++11 -o c c.cc

Error:

error: called object type 'Func<AAA, void, int>' (aka 'void (AAA::*)(int)') is not a function or function pointer

  myFunc(3);

  ~~~~~~^

1 error generated.

Upvotes: 1

Views: 66

Answers (1)

Swordfish
Swordfish

Reputation: 13144

Since dooo() is not a static member function of AAA you need an instance of AAA to call myFunc on:

AAA a;
(a.*myFunc)(3);

Upvotes: 4

Related Questions