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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:30:17+00:00 2026-05-10T14:30:17+00:00

As a general rule, I prefer using value rather than pointer semantics in C++

  • 0

As a general rule, I prefer using value rather than pointer semantics in C++ (ie using vector<Class> instead of vector<Class*>). Usually the slight loss in performance is more than made up for by not having to remember to delete dynamically allocated objects.

Unfortunately, value collections don’t work when you want to store a variety of object types that all derive from a common base. See the example below.

#include <iostream>  using namespace std;  class Parent {     public:         Parent() : parent_mem(1) {}         virtual void write() { cout << 'Parent: ' << parent_mem << endl; }         int parent_mem; };  class Child : public Parent {     public:         Child() : child_mem(2) { parent_mem = 2; }         void write() { cout << 'Child: ' << parent_mem << ', ' << child_mem << endl; }          int child_mem; };  int main(int, char**) {     // I can have a polymorphic container with pointer semantics     vector<Parent*> pointerVec;      pointerVec.push_back(new Parent());     pointerVec.push_back(new Child());      pointerVec[0]->write();      pointerVec[1]->write();       // Output:     //     // Parent: 1     // Child: 2, 2      // But I can't do it with value semantics      vector<Parent> valueVec;      valueVec.push_back(Parent());     valueVec.push_back(Child());    // gets turned into a Parent object :(      valueVec[0].write();         valueVec[1].write();          // Output:     //      // Parent: 1     // Parent: 2  } 

My question is: Can I have have my cake (value semantics) and eat it too (polymorphic containers)? Or do I have to use pointers?

  • 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. 2026-05-10T14:30:17+00:00Added an answer on May 10, 2026 at 2:30 pm

    Since the objects of different classes will have different sizes, you would end up running into the slicing problem if you store them as values.

    One reasonable solution is to store container safe smart pointers. I normally use boost::shared_ptr which is safe to store in a container. Note that std::auto_ptr is not.

    vector<shared_ptr<Parent>> vec; vec.push_back(shared_ptr<Parent>(new Child())); 

    shared_ptr uses reference counting so it will not delete the underlying instance until all references are removed.

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

Sidebar

Ask A Question

Stats

  • Questions 71k
  • Answers 71k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You can't do italics - but you can do bold;… May 11, 2026 at 1:18 pm
  • added an answer Here is a contrived example of how to do it.… May 11, 2026 at 1:18 pm
  • added an answer I'd recommend implementing StaffPersonnel, NonStaffPersonnel, and Vehicles as properties on… May 11, 2026 at 1:18 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.