I am using an array in a simple logic simulator program and I want to switch to using a vector to learn it but the reference I am using "OOP in C++ by Lafore" doesn't have a lot about vectors and objects so I am kinda of lost .
Here is the previous code :
gate* G[1000]; G[0] = new ANDgate() ; G[1] = new ORgate; //gate is a class inherited by ANDgate and ORgate classes class gate { ..... ...... void Run() { //A virtual function } }; class ANDgate :public gate {..... ....... void Run() { //AND version of Run } }; class ORgate :public gate {..... ....... void Run() { //OR version of Run } }; //Running the simulator using overloading concept for(...;...;..) { G[i]->Run() ; //will run perfectly the right Run for the right Gate type } Now what I want to do is
vector(gate*) G; ANDgate a G.push_back(a); //Error ORgate o G.push_back(o); //Error for(...;...;...) { G[i]->Run(); //Will this work if I corrected the error ?? } so can a vector array hold different types of objects(ANDgate , ORgate) but they inherit the type of the vector array (gate) ????
shared_ptrimplementation, either from Boost or TR1, or C++0x's<memory>. And for that kind of stuff you're doing, you might want to look into Boost pointer containers.