mercredi 6 décembre 2017

Function pointers in C++11

I am trying to understand function pointers and I have the following test code:

#include <iostream>

using namespace std;

class Test {
public:
    void test_func() {
        cout << "Test func called.";
    }
};

void outer_test_func(Test &t, void (Test::*func)()) {
    (t.*func)();
}

int main(int argc, char *argv[]) {
    auto t = Test();
    outer_test_func(t, &Test::test_func);
}

This works. But from what I understand Test::test_func and &Test::test_func both result in pointers. So why can't I use the former instead of the latter? If I try it g++ complains.

Aucun commentaire:

Enregistrer un commentaire