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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:34:41+00:00 2026-06-04T10:34:41+00:00

I have a single class Base, and a few tens of classes derived from

  • 0

I have a single class “Base”, and a few tens of classes derived from Base. I would like to have a method that creates me the right class by an index. Like this:

class Base
{
};

class A : public Base
{
}

class B : public Base
{
}

class C : public Base
{
}

Type array = { A, B, C };

and then I could do new array[i];

How could this be achieved with C++(0x)? Usually I would use an the Abstract Factory Pattern. But since I have a LOT of derived classes, this would really slow down the program.

Since the derived classes will be used only once I also taught to use this:

Base *array = { new A, new B, new C };

But this would lead to huge memory consumption, not counting that not every class will always be used.

Any suggestion?

  • 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-04T10:34:43+00:00Added an answer on June 4, 2026 at 10:34 am

    You cannot use an array of classes, but you can use an array of pointers to functions.

    typedef std::unique_ptr<Base> (*Creator)();
    
    template <typename T>
    std::unique_ptr<Base> make() { return new T{}; }
    
    Creator const array[] = { make<A>, make<B>, make<C> };
    
    int main() {
        std::unique_ptr<Base> b = array[1]();
    
        b->foo();
    }
    

    For those worried by the cost of creating so many template functions, here is an example:

    #include <stdio.h>
    
    struct Base { virtual void foo() const = 0; };
    
    struct A: Base { void foo() const { printf("A"); } };
    struct B: Base { void foo() const { printf("B"); } };
    struct C: Base { void foo() const { printf("C"); } };
    
    
    typedef Base* (*Creator)();
    
    template <typename T>
    static Base* make() { return new T{}; }
    
    static Creator const array[] = { make<A>, make<B>, make<C> };
    
    Base* select_array(int i) {
        return array[i]();
    }
    
    Base* select_switch(int i) {
        switch(i) {
        case 0: return make<A>();
        case 1: return make<B>();
        case 2: return make<C>();
        default: return 0;
        }
    }
    

    LLVM/Clang generates the following output:

    define %struct.Base* @select_array(int)(i32 %i) uwtable {
      %1 = sext i32 %i to i64
      %2 = getelementptr inbounds [3 x %struct.Base* ()*]* @array, i64 0, i64 %1
      %3 = load %struct.Base* ()** %2, align 8, !tbaa !0
      %4 = tail call %struct.Base* %3()
      ret %struct.Base* %4
    }
    
    define noalias %struct.Base* @select_switch(int)(i32 %i) uwtable {
      switch i32 %i, label %13 [
        i32 0, label %1
        i32 1, label %5
        i32 2, label %9
      ]
    
    ; <label>:1                                       ; preds = %0
      %2 = tail call noalias i8* @operator new(unsigned long)(i64 8)
      %3 = bitcast i8* %2 to i32 (...)***
      store i32 (...)** bitcast (i8** getelementptr inbounds ([3 x i8*]* @vtable for A, i64 0, i64 2) to i32 (...)**), i32 (...)*** %3, align 8
      %4 = bitcast i8* %2 to %struct.Base*
      br label %13
    
    ; <label>:5                                       ; preds = %0
      %6 = tail call noalias i8* @operator new(unsigned long)(i64 8)
      %7 = bitcast i8* %6 to i32 (...)***
      store i32 (...)** bitcast (i8** getelementptr inbounds ([3 x i8*]* @vtable for B, i64 0, i64 2) to i32 (...)**), i32 (...)*** %7, align 8
      %8 = bitcast i8* %6 to %struct.Base*
      br label %13
    
    ; <label>:9                                       ; preds = %0
      %10 = tail call noalias i8* @operator new(unsigned long)(i64 8)
      %11 = bitcast i8* %10 to i32 (...)***
      store i32 (...)** bitcast (i8** getelementptr inbounds ([3 x i8*]* @vtable for C, i64 0, i64 2) to i32 (...)**), i32 (...)*** %11, align 8
      %12 = bitcast i8* %10 to %struct.Base*
      br label %13
    
    ; <label>:13                                      ; preds = %9, %5, %1, %0
      %.0 = phi %struct.Base* [ %12, %9 ], [ %8, %5 ], [ %4, %1 ], [ null, %0 ]
      ret %struct.Base* %.0
    }
    

    Unfortunately, it is not quite intelligent enough to automatically inline the functions with a regular array code (known issue with the LLVM optimizer, I don’t know if gcc does better)… but using switch it is indeed possible.

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

Sidebar

Related Questions

I'm designing/building a system of classes that all derive from a single base class.
Scenario I have a single base class and 2 (possibly 3) other classes that
I have simple base class with single static field. I have numerous classes that
Currently I have created a ABCFactory class that has a single method creating ABC
I have a common base class from which all my ASMX webservice classes will
I have an abstract immutable base class that defines enforces child classes to be
I have multiple objects that inherit from a base class and am trying to
I have a base class Character which has several classes deriving from it. The
I have a Repository Class with the following method... public T Single<T>(Predicate<T> expression) {
For a newsroom system I have a class that contains a single news story.

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.