c++ - Returning a vector of unique_ptr by reference -
here code
class carl{ public: std::vector<std::unique_ptr<int>> asd; carl(){ asd.push_back(std::unique_ptr<int>(new int(4))); asd.push_back(std::unique_ptr<int>(new int(2))); } std::vector<std::unique_ptr<int>> & getvec(){ return asd; } }; int main(int argc, char** argv) { carl v; std::vector<std::unique_ptr<int>> * oi = &(v.getvec()); //how print first element? return 0; }
my goal access unique pointer w/o giving ownership.
my question that, if return reference , catch reference's address using vector pointer, work?
i know receiving reference work better, trying find out if it's possible receive pointer.
starting with:
std::vector<std::unique_ptr<int>> * oi
1) dereference pointer:
*oi;
2) access first element of vector:
(*oi)[0];
3) access int unique_ptr
points to:
*((*oi)[0]);
the above should work, not think solution. there wrong design when such solution seems necessary.
Comments
Post a Comment