0
#include <iostream>
#include <functional>
class Voice;
class EnvelopeMultiPoints
{
public:
std::function<double(Voice &, double)> mCallback;
void SetupModulation(std::function<double(Voice &, double)> callback, int paramID) {
mCallback = callback;
}
};
class Voice
{
public:
EnvelopeMultiPoints mEnvelopeMultiPoints;
};
class VoiceManager
{
public:
Voice mVoices[16];
inline void UpdateVoices(std::function<void(Voice &)> callback) {
for (int i = 0; i < 16; i++) {
callback(mVoices[i]);
}
}
static void SetupEnvelopeMultiPointsModulation(Voice &voice, std::function<double(Voice &, double)> callback, int paramID) {
voice.mEnvelopeMultiPoints.SetupModulation(callback, paramID);
}
};
class Oscillator
{
public:
double ModulatePitch(Voice &voice, double currentValue) {
// somethings with voice
return currentValue * 10.0;
}
};
int main()
{
VoiceManager voiceManager;
Oscillator *pOscillator = new Oscillator();
int param = 100;
auto callback = std::bind(&Oscillator::ModulatePitch, pOscillator, std::placeholders::_1, std::placeholders::_2);
voiceManager.UpdateVoices(std::bind(&VoiceManager::SetupEnvelopeMultiPointsModulation, std::placeholders::_1, callback, param));
Voice voice = voiceManager.mVoices[0];
std::cout << voice.mEnvelopeMultiPoints.mCallback(voice, 1.0) << std::endl;
delete pOscillator;
}
我创造一种语音更新“基本”的迭代器,我可以通过以后任何种类的功能。它迭代所有的声音并传递我需要的迭代函数。
但似乎我错了绑定Oscillator::ModulatePitch
函数传递给更新程序?
我在哪里错了?
是否允许使用lambdas?使用'auto callback = [pOscillator](自动&语音,双d){返回pOscillator-> ModulatePitch(语音,d); };'为我工作。一般的经验法则是当你可以使用lambdas时避免'std :: bind'。 – Maikel
你有没有考虑用lambdas替换'std :: bind'? – nwp
使用'callback'的显式类型将修复它。 'std :: function callback = ...' 但是我不能准确地说明为什么:) –
pergy