0

I would like some advice in working with object containing pointers to objects in a vector.

I would like to be able to manipulate specific elements of a vector. I tried to copy the data from the objects then I had trouble going back and ensuring the same elements were updated so I would like to simply return a pointer to that element of the vector manipulate that pointer and not have to ensure the correct elements get updated with the data.

I want to work with a handle for dynamic objects.

class h_AFV{
 AFV * pAFV;   
 int * cnt;

 friend class AFV;

 public:  

 h_AFV(int);
 h_AFV(int, int, int, int);   
 h_AFV(const h_AFV& afv) : cnt(afv.cnt), pAFV(afv.pAFV){++*cnt;}
 h_AFV& operator = (const h_AFV&);
~h_AFV();...

It is stored in:

   std::vector<h_AFV>AFVs;

I want to select a specific element to be returned:

h_AFV* player::gtAFV(int n){       
return  new h_AFV(AFVs[n]);     <-This is how I return a pointer to the element I want to manipulate
}

Then I want to add the pointer to another object with other pointers or data which will modify the selected element:

void control::moveOrder(int a, int sp , bool al){

  h_AFV * ph_AFV = NULL;
  if(currentPlayer == 1){ph_AFV = pl1->gtAFV(marker);}  <-This gets the pointer
  if(currentPlayer == 2){ph_AFV = pl2->gtAFV(marker);} 

  ph_AFV->setStat(1);   <-These work I tested the pointer with some output.
  ph_AFV->setOrder('m');

  mvOrder m(ph_AFV, a, sp, al);   

  checker(ph_AFV);
  moving.push_back(m);  


  delete ph_AFV;

}

I have added copy constructors and overloaded the assignment operators. Generally the code will work until the function goes out of scope or when I try to manipulate the stored data.

   void mvOrder::resolve(){
int rate = speed * .01;

        MessageBox(NULL, "Marker 1", "Marker!", MB_OK);

  int dir = pMover->gtFace() * .0174532925;   
  int DBI = pMover->gtDBI();
  int MaxSpd = data[DBI].gtRdSpeed();
  int spd = rate * MaxSpd;
        MessageBox(NULL, "Marker 2", "Marker!", MB_OK);
  int dist = spd;
  coord c = pMover->gtCoord(); 
  MessageBox(NULL, "Tank Moving", "Marker!", MB_OK);
  float tx = c.Xloc;
  float ty = c.Yloc;
  tx += (float)dist*(sin(dir));
  ty -= (float)dist*(cos(dir));
  int nx = tx;
  int ny = ty;
   pMover->setLoc(nx, ny);
   if(align){pMover->setAngle(dir);}
  else{pMover->setNAngle(dir);}

}  


 struct mvOrder{
   h_AFV * pMover;   

 int azmuth;
 int speed;
 bool align;   

 mvOrder();
 mvOrder(h_AFV*, int, int, bool);
 mvOrder(const mvOrder&);
 ~mvOrder();
  void resolve();
};

How can I accomplish this code and correctly manage the pointers?

  • In general is is not safe to to use pointer to objects in `std::vector`s. The container may reallocate and move things without warning. If you can guarantee that this does not happen then you can get away with it, but ... – dmckee --- ex-moderator kitten Sep 28 '13 at 23:16
  • Is it better to use arrays or a linked list? Such behavior would make the program unstable. Are there better containers or should I just use an array? There is a problem when I check to see if the object is already selected and stored. I would like to handle such input. If the object is selected then I want it removed before it is stored again. – Joseph Creaney Sep 28 '13 at 23:22
  • That depends on too many factors to offer a general answer. You must understand your needs in terms of access time, insertion times and so on. You might prefer some kind of hash-map or tree, but again: it depends. – dmckee --- ex-moderator kitten Sep 28 '13 at 23:35
  • Thanks for the advice. I did not know that vectors could play havoc with pointers. – Joseph Creaney Sep 29 '13 at 00:12

0 Answers0