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 8367631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:12:31+00:00 2026-06-09T13:12:31+00:00

Ok, situation: I am using a library that I have no control over, which

  • 0

Ok, situation:
I am using a library that I have no control over, which has a method createSomeObject().
This method returns a pointer to an abstract base class with pure virtual functions, and it has no copy constructor so I can’t instantiate it myself nor copy it (obviously).

I need to store some number (let’s say 10) of these in a vector, so I tried to do the following:

vector<AbstractBaseClass*> v(10);

for(int i = 0; i < 10; i++)
{
    v.push_back(library->createSomeObject());
}

As soon as this loop is over, the vector is filled with broken pointers.

I have tried the following:

vector<AbstractBaseClass*> v(10);

for(int i = 0; i < 10; i++)
{
    AbstractBaseClass* abc = library->createSomeObject();
    v.push_back(abc);
}

To no avail. I must be going crazy or doing something seriously wrong here. I’ve looked around but the answer is always use boost::shared_ptr. A great solution possibly, but I can’t guarantee that it’ll be on the machines that this will be built on, so I’d like to avoid packaging Boost with the code.

Is there something I’m missing? I feel as though I’m just forgetting some simple thing, as I can’t think of a reason one of these wouldn’t work.

  • 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-09T13:12:32+00:00Added an answer on June 9, 2026 at 1:12 pm

    When you do this:

    vector<AbstractBaseClass*> v(10);
    

    you’re already creating a vector which contains 10 (NULL) pointers. So after you’ve called push_back, you’ll have a vector with 20 pointers, out of which the first 10 are invalid.

    If you know the size beforehand:

    vector<AbstractBaseClass*> v(10);
    for(int i = 0; i < 10; i++)
    {
        v[i] = library->createSomeObject();
    }
    

    or, alternitively, call reserve after creating an empty vector.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following situation: I am using a third party .net library which
Hereis the situation: I have a folder that contains library DLLs, which are not
I have a library project which contains an abstract class, let's say ClassA .
The situation I am in is that I have written a perl script which
I have the following situation: Assembly A - Class library, contains my control Control
I have this open-source library that I'm having some trouble fixing a problem... This
I have a situation where I am using a library (collection of C++ header
Situation: I need to make an imap client (using java mail api) that if,
I have a situation where I will be using a repository pattern and pulling
in my python project I call a c++ dll using ctypes library. That c++

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.