pointer na metode do każdej klasy?

0

Oto co próbuję uzyskać:

class A
{

};
class B : public A
{
	void test();
};


int main(int argc, char* argv[])
{
	double (A::*funcPtr)() = B::test;
}

Czy jest jakaś metoda aby uzyskać wskaźnik na metodę nie ograniczając się do jednej klasy?

0

Powinno być void a nie double, ale mimo to nie działa.

1

tak, zwykły wskażnik na funkcję i w parze z nim std::bind

#include <iostream>
#include <functional>
#include <typeinfo>
using namespace std;

#define define_another(x) struct x{ void foo() const{ cout << typeid(*this).name() << endl; } }

define_another(A);
define_another(B);
define_another(C);

int main(){
	function<void()> funcs[] = {
		bind(&A::foo, A{}),
		bind(&B::foo, B{}),
		bind(&C::foo, C{})
	};
	
	for(auto func : funcs)
		func();
	return 0;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1