简体   繁体   English

C ++在不知道对象类型的情况下定义成员函数指针

[英]c++ defining a member-function pointer without knowing the type of the object

I know that the title is not very clear but I didn't know how to write it down in one sentence. 我知道标题不是很清楚,但我不知道如何用一句话写下来。 So the problem is that I want something like this: 所以问题是我想要这样的东西:

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

I know that is not going to work but I was wandering whether a solution exists for this problem in c++ or not? 我知道那是行不通的,但是我在徘徊在c ++中是否存在针对此问题的解决方案?

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: 'newline' : cannot convert from 'MainPage *' to 'std::remove_reference<_Ty> *' 错误C2440:“换行符”:无法从“ MainPage *”转换为“ std :: remove_reference <_Ty> *”

error C2647: '.*' : cannot dereference a 'void (__thiscall std::remove_reference<_Ty>::* )(int,int)' on a 'MainPage' 错误C2647:'。*':无法取消引用'MainPage'上的'void(__thiscall std :: remove_reference <_Ty> :: *)(int,int)'

是的,使用decltype

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM