typedef void (*callable_function)(double);
void call_function(callable_function func)
{
func(3.0);
}
class Foo;
union converter {
callable_function func;
void (Foo::*foo_func)(void);
};
class Foo {
private:
double d;
public:
Foo(void) : d(0.0)
{
converter c;
c.foo_func = &Foo::set_double;
call_function(c.func);//I know i can call the function directly, but that is not what i want to achieve
}
void set_double(double value)
{
d = value;
}
};
void main(void)
{
Foo foo;
}
尝试执行上面的代码时,我在以下行中收到堆损坏错误:
d = value;
.
可能是因为被调用的函数是类函数,而不是成员函数。
但是,如果我尝试将这一行更改:c.foo_func = &Foo::set_double;
to c.foo_func = &this->set_double;
,我会收到此编译器错误:
error C2276: '&' : illegal operation on bound member function expression
问题 #1:有没有办法将指针(可能是强制转换)从void(Foo::*)(void)
to转换为void()(void)
或修复编译器错误?