有没有办法使用= type赋值而不是这种语法:
void triggerAttack() { adsr.triggerAttack(); }
我希望做类似的事情:
void triggerAttack() = adsr.triggerAttack
std::function<void()> triggerAttack = adsr.triggerAttack
void(*triggerAttack)() = adsr.triggerAttack
但没有编译!
示例代码:
class LinearADSR
{
public:
void triggerAttack() { }
};
class JerobeamBlubb : public gen
{
public:
void triggerAttack() { adsr.triggerAttack(); }
protected:
LinearADSR adsr;
};
答案 0 :(得分:2)
通常,成员函数指针与通常的指针不同,因为它必须与其类的实例一起使用。
所以将代码更改为:
class LinearADSR
{
public:
void triggerAttack() { }
};
class JerobeamBlubb
{
public:
void (LinearADSR::*triggerAttack)();
protected:
LinearADSR adsr;
};
int main()
{
JerobeamBlubb a;
a.triggerAttack = &LinearADSR::triggerAttack;
}
关于失败的尝试:
void triggerAttack() = adsr.triggerAttack; is invalid syntax
std::function<void()> triggerAttack = adsr.triggerAttack
失败,因为triggerAttack
是成员函数,而不是通常的函数。您需要我之前解释过的类的实例。void(*triggerAttack)() = adsr.triggerAttack
因上述原因失败。