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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:18:34+00:00 2026-06-10T06:18:34+00:00

I have a question: class B : public class A { public: vector<int*> vec;

  • 0

I have a question:

class B : public class A {
public:
  vector<int*> vec; 
};

class A  {
};

vector<A*> vec_a;
vector<B*> vec_b;

if I push back an object of class B into both vectors.

B* b = new B;
vec_a.push_back(b);
vec_b.push_back(b);

then after that, I change something inside the object of class B,

such as:

int* i = ....
vec_b[0].push_back(i);

Does the vec_a change?

I am confused with that since I have checked that when vector push_back, it will only create a copy. But when I checked with the above codes, it changes. Are the two vectors hold a shared memory of object b?

Thanks

  • 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-10T06:18:36+00:00Added an answer on June 10, 2026 at 6:18 am

    Your vectors contain pointers to a common object. Therefore anything you change inside that object via a derefence of one of those pointers is reflected in the object that they are pointing to. Adding the pointers to the vectors creates a copy of the pointers themselves, not the object they point to. Had you added a common instance of class B to both vectors, each vector would contain a separate copy of that object. Adding pointers results copies of your original pointer being added to each vector – but both copies will have the same value and that is the memory address of the object they point to.

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

Sidebar

Related Questions

Well, the question is kinda simple. I have a object defined as: public class
I have simple type Question : public class Question { public string[] Tags {
I have a constructor question for C#. I have this class: public partial class
I have a question, here are two classes below: class Base{ public: virtual void
Basic C# syntax question: So I have this class public class BrandQuery<T> : Query<T>
I have an interesting question on initialization. I have the following code: public class
I have a few classes as shown here public class TrueFalseQuestion implements Question{ static{
Simple question on best practice. Say I have: public class Product { public string
Bit of a noob question but: I have a custom class defined as: public
Suppose you have a class NonCopyable class NonCopyable { public: NonCopyable(int n){} ~NonCopyable(){} [...]

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.