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?