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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:43:39+00:00 2026-05-16T18:43:39+00:00

This is a sequel to a related post which asked the eternal question: Can

  • 0

This is a sequel to a related post which asked the eternal question:

Can I have polymorphic containers with value semantics in C++?

The question was asked slightly incorrectly. It should have been more like:

Can I have STL containers of a base type stored by-value in which the elements exhibit polymorphic behavior?

If you are asking the question in terms of C++, the answer is “no.” At some point, you will slice objects stored by-value.

Now I ask the question again, but strictly in terms of C++11. With the changes to the language and the standard libraries, is it now possible to store polymorphic objects by value in an STL container?

I’m well aware of the possibility of storing a smart pointer to the base class in the container — this is not what I’m looking for, as I’m trying to construct objects on the stack without using new.

Consider if you will (from the linked post) as basic C++ example:

#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

}
  • 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-16T18:43:40+00:00Added an answer on May 16, 2026 at 6:43 pm

    Just for fun, based on James’s comment about a template-based system, I came up with this Vector-like implementation. It’s missing lots of features, and may be buggy, but it’s a start!

    #include <iostream>
    #include <vector>
    #include <boost/shared_ptr.hpp>
    
    template <typename T>
    class Vector
    {
    public:
        T &operator[] (int i) const { return p[i]->get(); }
        template <typename D>
        void push_back(D &x) { p.push_back(ptr_t(new DerivedNode<D>(x))); }
    
    private:
        class Node
        {
        public:
            virtual T &get() = 0;
        };
    
        template <typename D>
        class DerivedNode : public Node
        {
        public:
            DerivedNode(D &x) : x(x) {}
            virtual D &get() { return x; }
        private:
            D x;
        };
    
        typedef boost::shared_ptr<Node> ptr_t;
        std::vector<ptr_t> p;
    };
    
    ///////////////////////////////////////
    
    class Parent
    {
        public:
            Parent() : parent_mem(1) {}
            virtual void write() const { std::cout << "Parent: " << parent_mem << std::endl; }
            int parent_mem;
    };
    
    class Child : public Parent
    {
        public:
            Child() : child_mem(2) { parent_mem = 2; }
            void write() const { std::cout << "Child: " << parent_mem << ", " << child_mem << std::endl; }
    
            int child_mem;
    };
    
    
    int main()
    {
        Vector<Parent> v;
    
        v.push_back(Parent());
        v.push_back(Child());
    
        v[0].write();
        v[1].write();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the sequel to this question . I would like to combine three
In a sequel question to this question , my corporate environment lacks the libpython2.6.so
This is almost identical to this question asked by another user, and is the
This is a bit of a long shot, but if anyone can figure it
This might seem like a stupid question I admit. But I'm in a small
This is a difficult and open-ended question I know, but I thought I'd throw
This is my first post here and I wanted to get some input from
I have a view containing a UIWebView which is loading a google map (so
[for those not following along at home, this is the sequel to Rolling My
This is somewhat of a sequel to Slow Exists Check . Alex's suggestion works

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.