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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:29:21+00:00 2026-06-06T04:29:21+00:00

If I have a class called A and I have two classes that derive

  • 0

If I have a class called “A” and I have two classes that derive from it lets say “B” and “C” how do I create a new instance of both B and C not knowing which is which. This probably sounds confusing so let me clarify and use similar code as mine.

Base class:

class A {
    stuff...
}

Derived classes:

class B : A{
    stuff...
}

class C : A {
    stuff...
}

Then in some other class I have:

class Z {
    void Derp() {
        foreach(A classA in listOfA's) {
            create new instance of derived class
        }
    }
}

I simply cannot just say B = new B() and C = new C() because maybe B isn’t in the list at all or maybe there are three B’s and one A in the list. Surly there is some way to do it but if not I could have a switch-case statement like:

class Z {
    void Derp() {
        foreach(A classA in listOfA's) {
            switch(classA) {
                case is B:
                    B = new B();
                    break;
                case is C:
                    C = new C();
                    break;
                }
            }
        }
    }
}

I would like to avoid doing it this way unless necessary because every time I create a new class that derives from class A I’ll have to remember to add another case statement in there.

So how should I do this?

Thanks.

  • 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-06T04:29:22+00:00Added an answer on June 6, 2026 at 4:29 am

    A common C++ solution to this is the so-called virtual constructor idiom: you define a virtual function called clone in the base class A, and then override it in B to return a new instance of B and in C to return a new instance of C.

    If you need a blank instance instead of a clone, you could add a newInstance function that uses the default constructor.

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

Sidebar

Related Questions

I have a bas class called Media with two classes that inherit from it,
Say I have two classes, in two different headers, called: class TestA { public:
I have a class called Sprite, and ballSprite is an instance of that class.
I have a class called Entity, with two child classes: Creature and Item. (I'm
I have a two classes, a controller called AppController and a class called URLDelegate
I have two classes that don't know anything about themselfs class A, class B.
I have two classes called Person and Animal that have a lot of methods
I have a class called ItemBase from which a number of classes inherit. In
Lets say I have an abstract base class with a pure virtual that returns
I have a class called UserInfo that contains details about a given user. There

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.