I have a vector with shared pointers:
std::vector<std::shared_ptr<DescriptorsNs::Descriptor> > mDescriptorList;
Now i want to have a getter function that returns a vector with copies of those objects:
void CatUpdater::getDescriptorList(std::vector<Descriptor*>& descriptorList) const
{
descriptorList.clear();
for (auto it = mDescriptorList.begin(); it != mDescriptorList.end(); it++)
{
descriptorList.push_back(*it);
}
}
Now the problem is that Descriptor is an abstract class and when I try to add them to the vector it correctly states:
/usr/include/c++/4.7/ext/new_allocator.h|110|error: cannot allocate an object of abstract type ‘DescriptorsNs::Descriptor’|
I can solve this by trying a std::dynamic_pointer_cast for all types of derived classes but there should be an easier way of achieving this.
Can anyone tell me a better way of copying the original object to the returned vector ?
You can’t make Descriptor object since it is abstract.
But you can make a deep copy of pointers to these objects.
To do that your class Descriptor is missing
clonevirtual method!Add it to your class and implement properly in your derived.
And use clone() while making deep copy of one vector to another: