简体   繁体   English

来自不同类别的对象的向量

[英]Vector of objects from different classes

I'm trying to create a vector containing objects from different classes, derived from a base class. 我正在尝试创建一个包含来自不同类的对象的矢量,这些对象是从基类派生的。 Following answers to this question I've the following code which tries four different ways (commented out below); 在回答这个问题之后,我得到了下面的代码,它尝试了四种不同的方法(在下面注释); none of which will compile: 没有一个可以编译:

class observable {

    public:
    virtual void observe(alglib::complex_1d_array)=0;

    observable() {

    }
};

class Energy : public observable {

    public:
    void observe(alglib::complex_1d_array);

    Energy() {

    }
};

class ObservableCollection {

    private:
    int no_obs;
    vector<observable*> obs;

    public:

    ObservableCollection(vector<string> obs) {
        no_obs=obs.size();
        for(int i=0;i<no_obs;i++) {
            if(!strcmp(obs[i].c_str(), "energy")) {
                // option 1:
                // Energy E();
                // obs.push_back(E);

                // option 2:
                // obs.push_back<std::shared_ptr<observable>(new Energy())>;

                // option 3:
                // obs.push_back(new Energy());

                // option 4:
                // observable *E=new Energy();
                // obs.push_back(E);
            }
        }
    }


};

Any ideas? 有任何想法吗?

Option 1 cannot work because obs is a vector<observable*> ^. 选项1不起作用,因为obsvector<observable*> ^。 You cannot push a object of type observable because you can only store pointers ( observable* ) in it. 您不能推送observable类型的对象,因为您只能在其中存储指针( observable* )。 You could store such objects in a vector<observable> but you cannot store Energy objects in such vector. 您可以将这些对象存储在vector<observable>但不能在此矢量中存储Energy对象。

Option 2 cannot work because ecause obs is a vector<observable*> ^. 选项2无法使用,因为obsvector<observable*> ^。 You cannot push a object of type std::shared_ptr<observable> because you can only store raw pointers ( observable* ) in it. 您不能推送类型为std::shared_ptr<observable>的对象,因为您只能在其中存储原始指针( observable* )。 To store shared pointers, the vector must be of type vector<shared_ptr<observable>> . 要存储共享指针,向量必须为vector<shared_ptr<observable>>

Option 3 is effectively same as option 4. They would work if you actually tried to push into the member variable obj . 选项3与选项4实际上相同。如果您实际上尝试将其推入成员变量obj则它们将起作用。 However, you've hidden that variable by declaring a local variable by the same name (the function parameter). 但是,您已通过使用相同名称(函数参数)声明局部变量来隐藏该变量。 You cannot push observable* into a vector of strings. 您不能将observable*推入字符串向量。 The compilation error should be clear about this. 编译错误应该很清楚。 If you inteded to push into the member variable, you can use this->obs instead, or even better, use different name for the local variable. 如果您确实想推送到成员变量,则可以改用this->obs ,甚至更好的是,对局部变量使用不同的名称。

^Assuming you were actually pushing into this->obs rather than the local variable. ^假设您实际上是在使用this->obs而不是局部变量。

You are passing vector obs (called the same as member) and you are trying to push your class to it. 您正在传递矢量obs(与成员相同),并且您正在尝试将类推向它。 If you want to push to the member, use this.obs.push_back(). 如果要推送到成员,请使用this.obs.push_back()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM