This question already has an answer here:
- C++ smart pointer const correctness 4 answers
I'm having three cass members:
public:
std::vector<std::shared_ptr<Object> > getObjects();
std::vector<std::shared_ptr<const Object> > getObjects() const;
private:
std::vector<std::shared_ptr<Object> > m_objects;
I'm getting an compiler error when I'm return m_object in the const method(second) of getObjects because m_objects is not matching the return type(std::vector >).
In my workaround, I'm first reconstructing the object vector locally by iterating and then returning the local vector, but is there any more optimized way of handling this scenario?
std::vector<std::shared_ptr<const Object> > objects;
for (auto & object: m_objects)
{
objects.push_back(object);
}
return objects;
Aucun commentaire:
Enregistrer un commentaire