Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8617457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:51:47+00:00 2026-06-12T05:51:47+00:00

I have a vector with shared pointers: std::vector<std::shared_ptr<DescriptorsNs::Descriptor> > mDescriptorList; Now i want to

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:51:48+00:00Added an answer on June 12, 2026 at 5:51 am

    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 clone virtual method!

    Add it to your class and implement properly in your derived.

    class Descriptor {
     ...
      virtual Descriptor* clone() const = 0;
    };
    
    
    class SomeDescriptor : public Descriptor {
     ...
      virtual Descriptor* clone() const { return new SomeDescriptor (*this); }
    };
    

    And use clone() while making deep copy of one vector to another:

    void CatUpdater::getDescriptorList(std::vector<std::shared_ptr<DescriptorsNs::Descriptor> >& descriptorList) const
    {
        descriptorList.clear();
        descriptorList.reserve(mDescriptorList.size());
        for (auto it = mDescriptorList.begin(); it != mDescriptorList.end(); it++)
        {
            descriptorList.push_back((*it)->clone());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that requires a boost::variant containing shared pointers to various types
Let's say I have a container (std::vector) of pointers used by a multi-threaded application.
I have a resource_manager class which maintains a std::vector<boost::shared_ptr<resource> > internally. resource_manager is a
I have a class AudioDeviceManager that passes a std::vector by reference to an AudioAnalyzer
I have a vector of pointers like so: vector<Item*> items; I want to clear
i have a class that has this function: typedef boost::shared_ptr<PrimShapeBase> sp_PrimShapeBase; class Control{ public:
I have vector< pair<int, int>> myVec (N); I want to have all pairs initialized
I have the vector d<-1:100 I want to sample k=3 times from this vector
I have a vector of objects (objects are term nodes that amongst other fields
I have a struct that contains pointers: struct foo { char* f; int* d;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.