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?