stdvector - Copy from vector<pointer*> to vector<pointer*> in C++ -
i create vector , want copy vector b in class using below method, correct way? vector may destroyed! searched in google, not found solution , meaningful explanation. everyone
void stateinit(vector<cbutton*> listbtn) { _m_plistbtn = listbtn; };
yes , no, passing vector value:
void stateinit(vector<cbutton*> listbtn) { _m_plistbtn = listbtn; };
wich means listbtn copy of vector (asuming calling vector 1 passed parameter of stateinit), if delete vector a, vector b still have collection of pointers , valid since destruction of vector of pointers doesnt delete pointed objects because cant possible how (should call, delete, delete[], free?).
do keep in mind if modify/delete 1 of elements vector (using pointers on vector), element modified in vector b (since pointer same element).
im not sure intend this, if want copy whole vector, should implement clone mechanism objects , copy them using transform:
class clonefunctor { public: t* operator() (t* a) { return a->clone(); } }
then just:
void stateinit(vector<cbutton*> listbtn) { transform(listbtn.begin(), listbtn.end(), back_inserter(_m_plistbtn), clonefunctor()); };
if intention not clone share pointers should pass vector pointer or reference:
void stateinit(const vector<cbutton*>& listbtn) { _m_plistbtn = listbtn; };
Comments
Post a Comment