How to force implicit conversion in polymorphism?

前端 未结 1 2015
醉梦人生
醉梦人生 2021-01-23 01:53

Please consider this example, how do we force implicit conversion in function whose second parameter is pointer to member function. casting explicitly in argument list of functi

相关标签:
1条回答
  • 2021-01-23 02:37

    As clean as it gets. Code below. But I have no idea who's "this" pointer is going to be referenced when "temp" actually gets invoked.

    typedef void(Base::*polymorph)(); 
    
    void func(Base* arg1, polymorph arg2)
    { 
        polymorph temp = arg2;
    } 
    
    
    int main() 
    { 
        Derived* test = new Derived; 
        // first parameter work but another gives an error 
        func(test, static_cast<polymorph>(&Derived::f)); 
        delete test; 
        return 0; 
    } 
    
    0 讨论(0)
提交回复
热议问题