C++ same object in two different vectors -
i'm working on c++ project , wonder if it's possible store same object in 2 different vector. know how deal in c pointer, reference same object both table, i'm little bit confused in c++.
if create object , store in vector , in vector b. c++ copy object or it's same on both vector , if modify one, other modified ? in second case, take more place store twice (for accessibility issues) or it's not way deal ?
thanks.
cppreference great place check type of questions. let me quote relevant parts of link:
void push_back( const t& value );
void push_back( t&& value );
appends given element value end of container.
1) new element initialized copy of value.
2) value moved new element.
so yes, storing same element twice in 2 vectors cause copied twice. should use std::vector<t*>
if don't want waste memory. , always, should consider smart pointers (std::shared_ptr
/ std::weak_ptr
) instead of naked pointers.
Comments
Post a Comment