请考虑这个例子,
我们如何在第二个参数是指向成员函数的指针的函数中强制隐式转换。
在函数的参数列表中显式转换并不是我现在想要实现的目标。
相反,我希望编译器以某种方式这样做,就像使用第一个参数一样......
struct Base
{
virtual ~Base() = 0 {}
};
struct Derived : public Base
{
void f(){}
};
typedef void(Base::*polymorph)();
// how do I force IMPLICIT conversion here: EDIT: (polymorph type work only for polymorph pointer type no conversion)
void func(Base* arg1, polymorph arg2) // void* arg2, (void*) arg2 etc... dosn't work
{
polymorph temp = reinterpret_cast<polymorph>(arg2); // to achive this
}
int main()
{
Derived* test = new Derived;
// first parameter work but another gives an error
func(test, &Derived::f); // BY NOT CHANGING THIS!
delete test;
return 0;
}
尽可能干净。代码如下。但我不知道当“temp”实际被调用时将引用谁的“this”指针。
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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)