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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:35:10+00:00 2026-06-17T09:35:10+00:00

Here is the problem, I have a vector of pointers to an abstract base

  • 0

Here is the problem, I have a vector of pointers to an abstract base class filled with derived objects like so:

class AbstractBase { /* ... */ };  

clase Derived1 : public AbstractBase {
    Derived1() { }
    Derived1( const AbstractBase& abc ) { /* ... */ }
};

/* ... */

vector< AbstratcBase* > lThingies;

const AbstractBase& getThingie(int pos) {
    return *lThingies[pos];
}

And to get copies of the elements I use the derived class “copy constructor” like so:

Derived1 d1 = getThingie(2);

But the problem is that I don’t like this: There is no way to force the implementation of this “copy constructor” when building the class “derived42” and even when you do remember it’s very easy to get it wrong (like making it a recursive call, happened to me).

My question is: Is there a better way to do this? how?

Update:
One of the “unwritten” requirements of the solution that I’m looking for is to get a copy of the derived object as a local variable, so I don’t forget to delete it.

More info:
There are a number of lThingies and, at runtime, I can tell what are the derived objects contained within (derived1, derived2, etc.), but not at compile time.

  • 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-17T09:35:10+00:00Added an answer on June 17, 2026 at 9:35 am

    Do you know your getThingie returns a Derived1? If you know this, possibly you should change the return type of Derived1 and the storage in the std::vector to be Derived1 instead of AbstractBase.

    Assuming the above doesn’t work, you can do a Derived1 bob = dynamic_cast<Derived1&>(getThingie(...));, which does a run-time check that the thingy is a Derived1 or more derived instance, and if so it does the cast — if not, it throws an exception.

    If you don’t want to bother with the run time check because you are so certain that it is a Derived1, simply use Derived1 bob = static_cast<Derived1&>(getThingy(...));.

    However, working with actual instances of classes which have an inheritance hierarchy is very questionable. There are many, many things that can go wrong.

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

Sidebar

Related Questions

Here is my problem: I have an array of model class(Let's say, 'addressModel' with
Here is the question's plot: suppose I have some abstract classes for objects, let's
So here's what I got in a nutshell. I have a base item class
I cannot figure this out. I need to have an abstract template base class,
I have a base class, Organism, with 2 derived classes, Ant and Doodlebug. This
I have Derived Classes that inherit from a Base class with virtual functions. Im
I have problem adding arraylist to list view, will explain about my problem here..
First, sorry for my bad english, I'm French. Here the problem : I have
Here is the problem: I have two columns in a table that, for each
Here's the problem: I have a data-bound list of items, basically a way for

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.