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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:53:11+00:00 2026-06-10T18:53:11+00:00

What is the right thing to do? I know that if the container is

  • 0

What is the right thing to do? I know that if the container is of base class value type, then derived object stored is ‘sliced’. If container is of derived class type, then base class object can not be stored. Right?

If to go with pointers, auto_ptr can not be used because it’s have copy semantic problem. Is shared_ptr the only solutions to handle this problem?

Could anyone provide more details, sample code or online articles that address this issue? It should be quite a common question however i did not find much information on it in textbook or online.

Thanks in advance.

btw, i just search on unique_ptr. It does not seem to support copy semantics. So isn’t it only safe than auto_ptr to be used in STL, but maybe due to lack to copy semantics, many STL operation or algorithm can not be used on container of unique_ptr?

  • 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-10T18:53:13+00:00Added an answer on June 10, 2026 at 6:53 pm

    The most obvious solution would be to use std::unique_ptr:

    class IBase {};
    class Derived : virtual public IBase {};
    std::vector<std::unique_ptr<IBase>> v;
    v.push_back(std::unique_ptr<IBase>(new Derived())); 
    

    You could use std::shared_ptr, but it’s ownership semantics significantly change the program’s behaviour, keeping the dynamically allocated objects alive alive until nobody holds references to them.

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

Sidebar

Related Questions

I don't know if the title is right. The thing is that I have
Right now my implementation returns the thing by value. The member m_MyObj itself is
Just a thing that annoys me. When I right click on a method name
I have a container which holds a bunch of pointers to a base class,
From this answer: When is a C++ terminate handler the Right Thing(TM)? It would
In methods of controllers, there are usually many checks for doing the right thing.
The thing im trying to do right now is pulling in multiple links from
Here is the thing. Right now I have this e-commerce web site where people
I'm reading the book Spring Recipes right now, and am puzzled by one thing:
Right so I know my code/structure is pretty messy, I've not done MVC before

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.