C ++ defining a pointer to a member function without knowing the type of object

I know that the title is not very clear, but I did not know how to write it in one sentence. So the problem is that I want something like this:

void(typeof(this)::*function)(int,int); 

I know this will not work, but I wandered, is there a solution for this problem in C ++ or not?

Update:

 class MainPage { public: MainPage() { void (std::remove_reference<decltype(*this)>::*callback)(int, int) = &MainPage::myFunction; ((*this).*callback)(nullptr,nullptr); } ~MainPage() { } void myFunction(int a, int b) { } } 

Errors:

error C2440: 'new line': cannot be converted from 'MainPage *' to 'std :: remove_reference <_Ty> *'

error C2647: '. * ': cannot dereference' void (__thiscall std :: remove_reference <_Ty> :: *) (int, int) 'in' MainPage '

+4
source share
1 answer

Yes, use decltype :

 void (std::remove_reference<decltype(*this)>::type::*function)(int, int); 
+6
source

Source: https://habr.com/ru/post/1484082/


All Articles