3

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) ????

Ahmed
  • 3,398
  • 12
  • 45
  • 64
  • Please don't have manual memory management like that. At the *very* least get a `shared_ptr` implementation, either from Boost or TR1, or C++0x's ``. And for that kind of stuff you're doing, you might want to look into [Boost pointer containers](http://www.boost.org/doc/libs/1_43_0/libs/ptr_container/doc/ptr_container.html). – GManNickG Aug 13 '10 at 08:15
  • I don't know what is a shared_ptr :( and I don't understand the risks of that . – Ahmed Aug 13 '10 at 08:26
  • shared_ptr aren't risks, they are risk mitigators. They take care of deleting when coders forget to call delete on their new allocations – DumbCoder Aug 13 '10 at 08:30
  • Thanks GMan for the useful link – Ahmed Aug 13 '10 at 11:54

4 Answers4

4

You're half-way there:

std::vector<gate*> G;
G.push_back(new ANDgate);
G.push_back(new ORgate);
for(unsigned i=0;i<G.size();++i)
{
    G[i]->Run();
}

Of course, this way you need to take care to ensure that your objects are deleted. I'd use a vector of a smart pointer type such as boost::shared_ptr to manage that for you. You could just store the address of local objects (e.g. G.push_back(&a)), but then you need to ensure that the pointers are not referenced after the local objects have been destroyed.

Anthony Williams
  • 66,628
  • 14
  • 133
  • 155
1

Yes, that will work - as long as you make run() a virtual function in gate and use the address of operator(&) on a and o as you put them in the vector.

Be careful about object lifetime issues though. If a and/or o go out of scope then your vector will contain pointers to invalid objects.

pauljwilliams
  • 19,079
  • 3
  • 51
  • 79
1

Also, the base class "Gate" should have a virtual destructor else there would be issues while cleaning up the vector and it's contents.

Chubsdad
  • 24,777
  • 4
  • 73
  • 129
0

You are using

vector(gate*) G;

change to

vector<gate*> G;

and you should do this

G.push_back(new ANDgate());

or if you use boost use shared_ptrs as vector does quite a lot of copying and naked pointers in a vector can be fatal.

DumbCoder
  • 5,696
  • 3
  • 29
  • 40