首页 > 解决方案 > 如何定义和设置指向在中间类中传递两次的二维数组的指针

问题描述

我的 MainProcessor 中有一个数组

float waveforms[2][1080] = { {0}, {0} };

我将它传递给 SynthVoice,如下所示:

osc1Voice->setWavetable(waveforms, 0); //[1]
osc2Voice->setWavetable(waveforms, 1);

我在 SynthVoice.h 中这样读到它:

typedef float array_of_wavetable[1080];
void setWavetable(array_of_wavetable* waveform, int number)
    {
      wavetable = waveform;
      id = number;

}

所以我可以在我的 SynthVoice 类中这样使用它:

wavetable[id][first_sample + int(phase_0)]

现在我已经将 [1] 移到了一个新类 (OscilatorProcessor.h) 中,并且我想设置一个来自 MainProcessor -> OscillatorProcessor -> SynthVoice 的指针,以便我可以将其用作波表 [id] [相位]。

现在我不知道如何将它从我的 MainProcessor 传递到中间的 OscillatorProcessor 。所以我可以在 SynthVoice 类中以同样的方式阅读它。

我希望这是有道理的。感谢您的时间

编辑:最小的可重现示例

#include <iostream>

typedef float array_of_wavetable[1080];

class SynthVoice
{
public:
  void setWavetable(array_of_wavetable* waveform, int number)
  {
    wavetable = waveform;
    id = number;
  }

  void outputWavetable()
  {
    for(int i = 0; i < 1080; i++)
    {
      std::cout << wavetable[id][i];
    }
  }

private:
  array_of_wavetable * wavetable;
  int id;
};

class OscillatorProcessor
{
public:

  void setWavetable(array_of_wavetable * waveform, int number)
  {
    wavetable = waveform;
    id = number;
  }

  void sendWaveToVoice()
  {
    SynthVoice voice;
    voice.setWavetable(wavetable, id);
    voice.outputWavetable();
  }

private:
  array_of_wavetable * wavetable;
  int id;
};

int main() {
  float waveform_templates[4][1080] = { {0}, {0}, {0}, {0} };

  OscillatorProcessor oscillator;
  oscillator.setWavetable(waveform_templates, 2);

    return 0;
}

标签: c++arrayspointers

解决方案


推荐阅读