Reputation: 1778
I declare a shared_ptr to a vector of shared_ptr's to objects like so ...
std::shared_ptr<std::vector<std::shared_ptr<my_obj_t>>> vec =
std::make_shared<std::vector<std::shared_ptr<my_obj_t>>>();
My problem is I need to access the first element in the array used internally by the vector. Before changing this to a shared_ptr of objects, it was just an object and I used data().
But now with the vector of shared_ptr's, I'm not sure how to access that first element so that I can add an offset to it and return a pointer of my_obj_t.
I'm trying to use something like
(my_vec->data() + offset)
... where func requires a pointer to an array of my_obj_t's.
Thoughts?
Update: Before I was using it as ...
std::shared_ptr<std::vector<my_obj_t>> vec =
std::make_shared<std::vector<my_obj_t>>();
And I would access it by
(vec->data() + offset)
Upvotes: 0
Views: 1000
Reputation: 1778
Chris apparently didn't get around to making an answer, and since it was cleanest, this is what I used.
(*my_vec)[offset]->get()
Upvotes: 0
Reputation: 62531
To get a reference to the my_obj_t
pointed to from offset
*my_vec->at(offset)
There isn't an array of my_obj_t
anywhere
Upvotes: 1
Reputation: 20559
std::make_shared<std::vector<std::shared_ptr<my_obj_t>>>()
This constructs an empty vector of shared_ptr
s.
My problem is I need to access the first element in the array used internally by the vector.
Since the vector is empty, the internal array you are imagining probably doesn't exist yet. So there is no way to access the first element of it. You have to add the elements before trying to use them.
Upvotes: 0