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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:18:12+00:00 2026-06-19T03:18:12+00:00

I got a vector of pointers to a base class foo which got a

  • 0

I got a vector of pointers to a base class foo which got a couple of child classes and what I would like to do is based on which child class it is, create a new class of the same instance.

I’ve solved it earlier by having a gigantic for loop which uses typeid to find out what class it is but is there no way to solve it in a more general way?

Basically, something like this is what I’m looking for:

std::vector<foo*> a;
std::vector<foo*> b;

//Store a couple of classes in a and b

b[0] = new typeid(a[0]).name();
  • 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-19T03:18:13+00:00Added an answer on June 19, 2026 at 3:18 am

    One approach is to have a virtual clone() method that returns a pointer to an object of the right type:

    struct base 
    {
      virtual base* clone()=0;
      virtual ~base() {}
    };
    
    struct foo : public base
    {
      virtual base* clone() { return new foo(*this); }
    };
    
    struct bar : public base
    {
      virtual base* clone() { return new bar(*this); }
    };
    

    Then your code would be

    b[0] = a[0].clone();
    

    In real life code you would return a smart pointer, e.g. an std::unique_ptr<base>.

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

Sidebar

Related Questions

I've got a vector<MyType> and would like another vector<MyType> containing only those MyTypes which
I've got a vector of structs in C++ and I would like to modify
I've got a std::vector<Foo> where Foo is a class containing Foo( Foo&& ) noexcept
This question got me thinking about the max_size method in vector class. It is
I've got raw data streams from image files, like: vector<char> rawData(fileSize); ifstream inFile(image.jpg); inFile.read(&rawData[0]);
So here's what I got in a nutshell. I have a base item class
I need to keep a list(vector) of children for every class, and I've got
I've got vector A which contains numbers 1,7,9,9,3,13,3 I've got vector B which contains
i have got an own vector.class and a polygon.class Vector.class package org.onvif.ver10.schema; import javax.xml.bind.annotation.XmlAccessType;
I have got the following in a header file for a class: #include <vector>

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.