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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:01:04+00:00 2026-06-02T23:01:04+00:00

I have two classes like this (simplified and renamed): class Base { public: virtual

  • 0

I have two classes like this (simplified and renamed):

class Base
{
 public:
  virtual void operator()( ) { cout << "Base\n";}
};

class Derived : public Base
{
 public:
  void operator()( ) { cout << "Derived\n"; }
};

void doSomething( Base obj )
{
  obj( );
}

int main( )
{
  list<Base> bList;
  Derived d1, d2, d3;
  bList.push_back( d1 );
  bList.push_back( d2 );
  bList.push_back( d3 );

  for( list<Base>::iterator it = bList.begin(); it != bList.end(); it++ )
    {
      doSomething( *it );
    }
  return 0;
}

When I run this, I get:

Base
Base
Base

i.e. the Base::operator() is called. Obviously this is not what I want to happen.
I tried changing doSomething to

void doSomething( Base& obj )

but this only works if called directly with a Derived object and not with one from the list. And references can’t be stored in a list.
Is there any way how I can make doSomething “see” that it has to call a derived class’ function (without having to resort to pointers)?

Edit: Problem is pointed out, for other people who might run into this here is a link:
What is object slicing?

  • 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-02T23:01:05+00:00Added an answer on June 2, 2026 at 11:01 pm

    The problem isn’t that the compiler doesn’t “see” that it needs to call a function in a derived class, the problem is that you are only storing the base class in your container list<Base>. This results in what is commonly called object slicing. When the compiler copies your Derived objects into the container that can only hold Base objects, it can only copy the base portion of the object and “slices” off the part that makes it a Derived object.

    In order to make use of polymorphism with containers that follow value semantics (ie, assume that you store the actual object in them and not a form of reference to an object), you need store a pointer to said object. I would advise against storing raw pointers in a standard library container as it introduces additional lifecycle management issues – you’d have to control the lifetimes of the pointed-to objects from outside the container, which tends to be a really bad idea and usually either leads to memory leaks or dangling pointers, if not both.

    My recommendation would be that if you can use boost, use one of the ptr_container template classes that were explicitly designed for this purpose. If you can’t and you’re on a modern enough compiler, you can use a std::list<std::shared_ptr<Base> > > and rely on the shared_ptrto manage the objects’ lifecycles.

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

Sidebar

Related Questions

my code is like this: i have two classes first class: public class Box<E>
Lets say I have a two classes like this: public class LocalResources { public
Let's say I have two classes that look like this: public class ByteFilter {
I have two connected classes defined like this: [ActiveRecord] public class Store : ActiveRecordBase<Store>
I have two classes declared like this: class Object1 { protected ulong guid; protected
I have two classes, Foo and Bar, that have constructors like this: class Foo
So I have two classes like this: class foo { /* code here */
I have two classes : import android.cla; public class CW { public static void
Say I have two classes like this: class A{ private static Random random =
I have two classes, Person and Company, derived from another class Contact. They are

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.