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

  • Home
  • SEARCH
  • 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 8672085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:06:50+00:00 2026-06-12T19:06:50+00:00

I am loading different types of derived classes from a file, which are of

  • 0

I am loading different types of derived classes from a file, which are of the form:

  • 4-byte class ID header
  • n-byte serialized data

Each of the classes are inherited from a same base class, but I have some trouble creating them elegantly. This is my current code (here Foo and Bar are inherited from the same type):

// read class ID
uint32_t id = ReadHeader(myFile);

// create correct class
switch (id)
{
    case ID_CLASS_FOO: myClass = new Foo(myFile); break;
    case ID_CLASS_BAR: myClass = new Bar(myFile); break;
    /* ... */
}

But I find this rather ugly, tedious and prone to error, since for every extra class I add, I need one extra define/enum member, and one additional line in the switch.

What I am looking for is something where I would declare a compile-time “type array” like such:

ClassTypes = {Foo, Bar, ...};

And then, when reading the file, just go:

myClass = new ClassTypes[id](myFile);

Is there some way to do this in C++?

  • 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-12T19:06:51+00:00Added an answer on June 12, 2026 at 7:06 pm

    You could create a factory class.

    Factory definition:

    typedef ClassType* (*ClassCreation(void))
    
    class ClassFactory
    {
        private:
            map<ClassId, ClassCreation> creators;
    
        public:
            ClassFactory()
            {
                creators[ID_CLASS_FOO] = &Foo::create;
                creators[ID_CLASS_BAR] = &Bar::create;
            }
    
            ClassType* getClassType(ClassId id)
            {
                return (creators[id])()
            }
    };
    
    class ClassType
    {
        //etc
    };
    
    class Foo : public ClassType
    {
        public:
            static ClassType* create()
            {
                return new Foo;
            }
    };
    
    class Bar : public ClassType
    {
        public:
            static ClassType* create()
            {
                return new Bar;
            }
    };
    

    Factory use:

    ClassFactory factory;
    
    uint32_t id = ReadHeader(myFile);
    
    ClassType* myClass = factory.getClassType(id);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Step 1: Loading data via bulk insert from .txt (delimited) file into Table 1
I'm dynamically loading different content from db, using ajax. So, when I'm loading plain
I have an asp.net website with different user types like admin, registered user. Each
Summary: In search of the standard C++ design pattern for loading different files via
The images iam loading are slightly different sizes. how can i get the smallest
Error loading script occurs under FireFox 3 if I quickly click different links on
Loading class (View): public class Loading extends View { private long movieStart; private Movie
When loading the class, using method forName() , any static initializers in the class
While loading a Windows Form (C#), I'm trying to iterate through all controls. So
What are the differences between these 3 types of loading? Can someone explain with

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.