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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:10:56+00:00 2026-06-03T20:10:56+00:00

I came across a weird situation today while coding and I’m hoping someone could

  • 0

I came across a weird situation today while coding and I’m hoping someone could shed some light onto why this is happening.

I have a list of pointers to some base class:

std::list<BaseClass*> m_list;

Then I get one of the BaseClass pointers from this list

BaseClass* pBase = m_list.front();

Then I turn this base class into one of its child classes. (This is where I think the weirdness comes into play)

pBase = new ChildClass(*pBase);

The ChildClass uses the BaseClasses copy constructor to copy over all of BaseClasses fields.

Now with this ChildClass I call one of BaseClasses methods to set a field in BaseClass.

pBase->SetSomeIntMember(10);

Now if I check this int value it is 10 as expected, but it appears to only be changing it locally because if I get this same ChildClass from the list again and check it’s int member it will be unchanged.

Hopefully this wasn’t too tricky to follow. What makes this happen? In any situation where there is no polymorphism involved it would obviously not only be a local change as we have a pointer to the class instance. I’m guessing that I’m stomping on the pointer when I create a new ChildClass, but it definitely makes the BaseClass from the list become a ChildClass because the virtual methods still work.

  • 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-03T20:10:59+00:00Added an answer on June 3, 2026 at 8:10 pm

    You copy the value of the pointer, not a reference to the pointer.

    That is,

    BaseClass* pBase = m_list.front();
    pBase = new ChildClass(*pBase);
    

    is not the same as

    Baseclass*& pBase_r = m_list.front();
    pBase_r = new ChildClass(*pBase_r);
    

    Remember, if you want to update the original value, you need to use references or pointers.

    Note

    The second example contains a memory leak since the original value of pBase is discarded before delete. To avoid such surprises, use smart pointers, e.g. std::shared_ptr<T> (C++11) or boost::shared_ptr<T> in place of T*.

    Do not use std::auto_ptr<T> because its semantics are not compatible with STL containers.

    So your list class should be std::list<std::shared_ptr<BaseClass>>. Another advantage here is that you can use instances of the smart pointer instead of references without messing up the internal reference count.

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

Sidebar

Related Questions

I came across some weird behavior today around creating an object from the return
I just came across a weird error: private bool GetBoolValue() { //Do some logic
I came across something weird today, and I was wondering if any of you
I recently came across some weird looking class that had three constructors: class Class
Came across the term pigeon computing while reading about how google works.. Can someone
I came across a weird situation when trying to count the number of rows
I just came across this weird behavior today: interface IFooBar { void Foo(); void
We just came across a weird bug in our application that uses the asp.net
I came across the following weird chunk of code.Imagine you have the following typedef:
I came accross a weird problem, I want to do some basic math checks.

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.