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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:20:55+00:00 2026-06-12T14:20:55+00:00

I want to have a container (let’s say an std::vector) that would hold various

  • 0

I want to have a container (let’s say an std::vector) that would hold various inherited types, and would instantiate them,.i.e. vector of classes –> vector of objects.

For instance:

class A{};

class B: public class A
{};

class C: public class A
{};

void main()
{
    std::vector<of inherited A types> typesVec;
    std::vector<A*> objectsVec;

    typesVec.push_back(class B);
    typesVec.push_back(class C);

    for (int i = 0; i < typesVec.size(); i++)
    {
        A* pA = new typesVec.at(i);
        objectsVec.push_back(pA);
    }

}

Thanks in advance..

  • 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-12T14:20:57+00:00Added an answer on June 12, 2026 at 2:20 pm

    This isn’t possible in C++ (at least not directly). I can see this happening in a language that has reflection, but C++ doesn’t.

    What you can do instead is create a factory or simply methods that create objects of the specified type.

    Instead of having a vector of types, you’d have a vector of object generators (close enough, right?):

    class A{};
    
    class B: public class A
    {};
    
    class C: public class A
    {};
    
    struct AFactory
    {
        virtual A* create() { return new A; }
    };
    struct BFactory : AFactory
    {
    
        virtual A* create() { return new B; }
    };
    struct CFactory : AFactory
    {
    
        virtual A* create() { return new C; }
    };
    
    //...
    
    typesVec.push_back(new BFactory);
    typesVec.push_back(new CFactory);
    
    for (int i = 0; i < typesVec.size(); i++)
    {
        A* pA = typesVec.at(i)->create();
        objectsVec.push_back(pA);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a container (std::vector) of pointers used by a multi-threaded application.
Let say we are in landscape and I have one root container in that
I have two containers, let's say they're defined like this: std::vector<std::unique_ptr<int>> a; std::vector<std::unique_ptr<int>> b;
Let's consider we have QWidget that contains QTableWidget (only). So we want to resize
I have a container element that you can drag objects around in. I want
I have an ImageButton that I want to fill its parent RelativeLayout container for
Have three divs in a container that I want to float over a large
I have a stream of data that I want to place into a container.
I have a class with a std::map used as a container. I want to
Let's say I want to use an EJB container and I don't know how

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.