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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:12:26+00:00 2026-06-04T20:12:26+00:00

I have some code which is like follows: MyClass* a = new MyClass(); vector[0]

  • 0

I have some code which is like follows:

MyClass* a = new MyClass();
vector[0] = *a;

MyClass KeepCopy = vector[0];

//
//
vector gets changed
//
//

return KeepCopy

Now vector then goes and does lots of stuff and gets overridden. However, I wish to return the value of KeepCopy at the end of my function. However, because vector[0] is no pointing to something else I think I am being returned the wrong data.

EDIT3: Ok so this is whats happening. KeepCopy gets assigned to vector[0] and then in the code I do stuff to vector[0]. I was thinking that KeepCopy still points to a version of vector[0], but it appears KeepCopy only contains a copy of the value AT THE TIME which the assignment was made.

Later on I assign vector to another vector object (I am reading a text file two lines at a time, vector contains one line and this other vector object contains the second line). How can I make sure KeepCopy points to the original vector[0] element (before I start parsing through each line in the textfile, reassigning the vectors)?

  • 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-04T20:12:27+00:00Added an answer on June 4, 2026 at 8:12 pm

    As you are storing the object by value (and not by pointer/referenence), changes to vector[0] are not reflected in KeepCopy, unless you share pointers to the same contained object in MyClass instances

    If this is the case, you should implement a proper copy constructor that makes a deep copy of the original (instead of the default shallow copy that just blindly copies the values of pointers)

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

Sidebar

Related Questions

I have some code which is been running by a backgroundworker I'd like some
I have some Java code which looks roughly like this: String urlString = ftp://polar.ncep.noaa.gov/pub/history/waves/multi_1.glo_30m.dp.200601.grb2;
I have some auto-instantiation code which I would like to apply to about 15
I have been given some generated code which looks like so: <form id=form1 method=post
I have some code which builds an array of date ranges. I then call
I have some code which fetches an XML file from a URL and then
I have some code which populates a hashtable with a question as the key
I have some code which handles data files and reports an error when it
We have some code which sorts a list of addresses based on the distance
I have some code which is built both on Windows and Linux. Linux at

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.