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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:54:52+00:00 2026-06-11T08:54:52+00:00

Apologies if I’m missing a fairly fundamental concept here, but I’m trying to work

  • 0

Apologies if I’m missing a fairly fundamental concept here, but I’m trying to work out how to maintain a collection of multiple class types (all derived from the same parent) and still have access to their subclass-specific methods when retrieving them from the collection.

As context, I have one base class (“BaseClass”) and a number of classes (say “SubClassA” through “SubClassZ”) that all derive from this base class. I also need to maintain a central indexed collection of all SubClass objects, which I have created as

typedef unordered_map<std::string, BaseClass*> ItemCollectionType;
ItemCollectionType Items;

The list will only ever contain SubClass objects but I’ve defined the list member type as a “BaseClass” pointer so that it can store instances of any sub class. So far, I’m assuming there’s nothing particularly bad about this approach (but please correct me if I’m wrong).

The issue I have is that sometimes I need to create a copy of these collections and their contents. The (simplified) code I have for this is as follows:

ItemCollectionType CopiedItems;
ItemCollectionType::const_iterator it_end = Items.end();

for (ItemCollectionType::const_iterator it = Items.begin(); it != it_end; ++it) 
{
    BaseClass *src = it->second;
    BaseClass *item = new BaseClass(src);
    NewItems[item->code] = item;
}

The obvious issue here is that the compiler will only call the BaseClass copy constructor, rather than that of the SubClass that “src” actually points to, because it does not know what type of SubClass it is.

What is the recommended approach for a situation like this? Should I even be in a situation where I “lose” this sub class type information by adding to a collection of type BaseClass? I can’t see any other way to maintain the collection but please let me know of any better alternative.

Each SubClass object does contain a numeric ID that indicates which flavour of sub class it is; as a result, is it possible to write a function that translates this numeric ID into a “type” object, which can then be used to cast the source object before calling the copy constructor? Is this a valid use for templating?

Of course, this could be terrible programming practice and/or not possible. I’m just certain there should be a better option than the simplest and least-maintainable solution of

switch (src->code)
{
    case SubClassTypes::VarietyA:
        SubClassA *item = new SubClassA( *((SubClassA*)src) );
        Items[item->code] = item;
        break;
    case SubClassTypes::VarietyB:
        SubClassB *item = new SubClassB( *((SubClassB*)src) );
        Items[item->code] = item;
        break;
    ...
    ...
}
  • 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-11T08:54:53+00:00Added an answer on June 11, 2026 at 8:54 am

    You can define abstract Clone method which will be implemented in each subclass.

    virtual BaseClass* Clone() = 0;
    

    Than just call:

    for (ItemCollectionType::const_iterator it = Items.begin(); it != it_end; ++it) 
    {
        BaseClass *src = it->second;
        NewItems[item->code] = src->Clone();
    }
    

    Also note that C++ support return value covariance so your derived classes can return the exact type and not just the base class, e.g.

    // in DerivedClass (which derives from BaseClass)
    DerivedClass* Clone() { ... } 
    

    This is considered a proper override, although the return type is not the same as in the base class.

    Casting the base class pointers to derived classes, based on an attribute, is not considered good OOP practice.

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

Sidebar

Related Questions

Apologies if I'm missing something really obvious, but I'm trying to understand how to
Apologies but I seem to be going round in circles. The process is fairly
Apologies if I'm missing something obvious, but when I create a new type with
apologies for having to create a new thread for this but I'm starting out
Apologies for the somewhat vague title, I can't work out what the keywords are
Apologies for the vagueness of the question here but can someone point me to
Apologies for a simple question, but I'm very new to Perl! I have an
Apologies if this is classic ASP 101, but its been so long since I
Apologies or asking what is probably a very straightforward question, but I'm new to
Apologies if I've framed the question incorrectly but I'm not sure where it fits

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.