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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:55:20+00:00 2026-05-24T09:55:20+00:00

Is it possible to store a bunch of objects by their base class in

  • 0

Is it possible to store a bunch of objects by their base class in say an std::list without pointers. I would really like the objects to be held in the container, then retrieve a pointer to the object in the container and dynamic_cast it to correct derived class.

I have it working fine using pointers. Like (super simple version):

class IComponent  
{   
    virtual ~Icomponent(){}  
}  

class PositionComponent: public IComponent  
{  
    //...  
    float x, y;  
}

std::list<IComponent*> CList;

//...  
// fill the list  
// put reference to object in pComponent
//...   

PositionComponent* position = dynamic_cast<PositionComponent*>( pComponent) 

position->x = 346452.235612;

But the memory management is a huge pain. My actual structure is a

map<enumValue, map<int, IComponent*> >

I get the feeling I can’t use the objects themselves because when I add any derived component into the list the extra data will be cut off and leave me with the base class only. This didn’t figure this until I tried static_cast instead and it crashed.

Can answer my original question and/or confirm my feelings on the matter. 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-05-24T09:55:21+00:00Added an answer on May 24, 2026 at 9:55 am

    As you guessed, when you stored an object in a container by value, it gets sliced and the data is chopped off.

    If you only need to store one data type (you only show one in your code), then you can make the container hold that type.

    If not, you really are stuck using pointers. You can make the memory management much easier by using a smart pointer, or if appropriate, a boost ptr_container of some sort.

    Further you might want to think if you need to spend one more iteration considering your design to provide an interface that doesn’t require doing a dynamic_cast to get the original type back out again.

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

Sidebar

Related Questions

Lets say I have the following code: public class Base { // Some stuff
I'm using a std::deque to store a fairly large number of objects. If I
Is it possible to store objects in globalStorage or is it only capable of
Is it possible to store encrypted connection string so it can be used from
Is it possible to store passwords on the local system (Windows XP) that can
Is it possible to store the results of a call to exec sp_executesql in
I was talking to somebody a recently who mentioned it was possible to store
A few days ago, I asked why its not possible to store binary data,
Is it possible to query certificate store on windows server 2008 using .net platform?
Is it possible to specify a relative connection string for an AzMan XML store?

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.