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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:10:58+00:00 2026-06-08T21:10:58+00:00

I have a Component class, which is the parent class, and for example a

  • 0

I have a Component class, which is the parent class, and for example a Sprite class. Sprite also has various children for animation, but that is not the focus of this question. I also have a component manager which holds a map<string, Component>. I store every component I create in this map. Currently there are 12 types of components, but that will increase.

My Component class has virtual void draw() and update() functions, and the same for Sprite in this case. When I loop through the map of components, however, it only calls the Component draw() and update().

I haven’t really been able to find an answer that has actually worked for me yet.

  • 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-08T21:10:59+00:00Added an answer on June 8, 2026 at 9:10 pm

    You’re experiencing object slicing—since your map only stores Component objects by value, those objects do not include any of the information provided by Component subclasses. Generally speaking, subtype polymorphism only works through pointer types, not value types. So you need to store the instances by pointer, preferably std::map<std::string, std::unique_ptr<Component>>:

    components["example"] = std::unique_ptr<Component>(new Example());
    

    If you are using a non-C++11 compiler, use std::map<std::string, Component*>:

    components["example"] = new Example();
    

    And make sure you delete the Component objects during cleanup, e.g., in the destructor of your component manager. C++03:

    for (std::map<std::string, Component*>::const_iterator i = components.begin();
        i != components.end(); ++i)
        delete i->second;
    

    C++11:

    for (const auto& i : components)
        delete i.second;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class called Component which has a number of classes derived
I have a class which is routing many messages into inner component instance. So
I have a base class called Component. I also have 2 interfaces I2DComponent, and
I have a GUI tool that manages state sequences. One component is a class
I have a component which has a corresponding persistent. This persistent is of course
I have a component (link to Test class) which contain one line in it,
I have a class which I would like to map as a component onto
I have: @Component class MyDecorator{ private Cache cache; /* some wrapped methods like get
I have created a Class Library (Core Processing Component) using C# in Visual Studio
I have two spring beans as follows: @Component(A) @Scope(prototype) public class A extends TimerTask

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.