在此类中设置变量,以便可以为每个对象声明它们

时间:2018-09-30 18:18:35

标签: c++ class object variables juce

我正在JUCE中使用C ++,但这是一个一般的初学者C ++问题。

我上了这个课:

class LabeledGroup : public GroupComponent

{
public:
    LabeledGroup(const String& name)
    {
        setText(name);
        setTextLabelPosition(Justification::centredTop);

        addAndMakeVisible(dummy1);
        addAndMakeVisible(dummy2);
        addAndMakeVisible(dummy3);
        addAndMakeVisible(dummy4);

        }
    }

    void resized() override
    {

        FlexBox knobBox1;
        knobBox1.flexWrap = FlexBox::Wrap::wrap;
        knobBox1.justifyContent = FlexBox::JustifyContent::flexStart;
        knobBox1.alignContent = FlexBox::AlignContent::flexStart;

        Array<LabeledSlider*> knobs1;
        knobs1.add(&dummy1, &dummy2, &dummy3, &dummy4);

        for (auto *k : knobs1)
            knobBox1.items.add(FlexItem(*k).withMinHeight(80.0f).withMinWidth(80.0f).withFlex(1));

        FlexBox fb1;
        fb1.flexDirection = FlexBox::Direction::column;
        fb1.items.add(FlexItem(knobBox1).withFlex(2.5));
        fb1.performLayout(getLocalBounds().toFloat());

    }


private:
    LabeledSlider dummy1{ "Dummy 1" };
    LabeledSlider dummy2{ "Dummy 2" };
    LabeledSlider dummy3{ "Dummy 3" };
    LabeledSlider dummy4{ "Dummy 4" };

};

哪个对象将在其中创建4个虚拟“ LabeledSlider”对象,使其可见,并使用FlexBox函数对其进行组织。

当我在“ MainContentComponent”(这是我的程序在JUCE中以可视方式构建的地方)中调用此类时,它可以正常工作:

class MainContentComponent : public Component

{
public:
    MainContentComponent()

    {
        addAndMakeVisible(group1);
    }

private:
    LabeledGroup group1{ "Group 1" };

当然,问题是我直接在LabeledGroup类内部创建并命名了所有“虚拟旋钮”,而我需要为该类的每个对象设置不同的名称。

即。我只需要在MainContentComponent中指定:

  • LabeledGroup group1{ "Group 1" }包含“ dummy1,dummy2,dummy3,dummy4”
  • LabeledGroup group2{ "Group 2" }包含“ dummy5,dummy6,dummy7”。

执行此操作的好方法是什么?

2 个答案:

答案 0 :(得分:0)

每个评论请求:

#include<array>
#include<string>
std::array<LabeledSlider,4> genLabels(int startIndex)
{
     std::string dummy="Dummy";
     return std::array<LabeledSlider{
     LabeledSlider(dummy+std::to_string(startIndex)),
     LabeledSlider(dummy+std::to_string(startIndex+1)),
     LabeledSlider(dummy+std::to_string(startIndex+2)),
     LabeledSlider(dummy+std::to_string(startIndex+3))};
}
class LabeledGroup : public GroupComponent

{
public:
    LabeledGroup(const String& name, int startIndex)
    :dummies(genLabels(startIndex))
    {
        setText(name);
        setTextLabelPosition(Justification::centredTop);
        for(auto&& d: dummies)
            addAndMakeVisible(d);
    }

    void resized() override
    {

        FlexBox knobBox1;
        knobBox1.flexWrap = FlexBox::Wrap::wrap;
        knobBox1.justifyContent = FlexBox::JustifyContent::flexStart;
        knobBox1.alignContent = FlexBox::AlignContent::flexStart;

        for (auto&& d : dummies)
           knobBox1.items.add(FlexItem(d).withMinHeight(80.0f).
                               withMinWidth(80.0f).withFlex(1));

        FlexBox fb1;
        fb1.flexDirection = FlexBox::Direction::column;
        fb1.items.add(FlexItem(knobBox1).withFlex(2.5));
        fb1.performLayout(getLocalBounds().toFloat());

    }


private:
    std::array<LabeledSlider,4> dummies;

};

答案 1 :(得分:0)

我建议这样的事情

#include<string>
#include<initializer_list>
#include <vector>

struct LabeledSlider {
    LabeledSlider (std::string ) {}
};

struct LabeledGroup {
    LabeledGroup(std::string name,
                 const std::initializer_list<std::string> slidersName) {
        for (auto &name: slidersName)
            sliders.push_back(LabeledSlider(name));
        for (auto &s : sliders)
              addAndMakeVisible(s);
    }

    std::vector<LabeledSlider> sliders;
};

int main() {
   LabeledGroup lg1("coucou", { "riri", "fifi", "loulou" });
   LabeledGroup lg2("hello", { "sam", "max" });
   LabeledGroup lg3("hello", { });
}