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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:10:58+00:00 2026-05-23T07:10:58+00:00

Im having some difficulties when passing a reference to a vector as an argument.

  • 0

Im having some difficulties when passing a reference to a vector as an argument. Initially it was a pointer to a vector of pointers, and the functions called would change attributes of whatever obejct was calling it. For example I used:

(*myVector)[i]->myFunction();

and everything worked perfectly. myFunction() would translate whichever object was the current index of myVector.

Now i’ve changed the vector to hold a set of objects instead of pointers to the objects as it did originally. Instead of passing a pointer to the vector, i pass a reference of it. I access the function as follows:

myVector[i].myFunction();

It compiles, I find that myFunction() no longer has the same results as it did before. I used a breakpoint and saw that it was changing attributes it should have been, however, those changes never made it to the original object. There was no final translation even though the breakpoint showed the origin of myVector[i] being changed.

I’ve tried passing a pointer to the vector of objects and used:

(*myVector)[i].myFunction();

and the same thing, it compiled but didn’t work.

Finally i tried simply passing a reference to the first object in the vector, and of course that worked just fine:

myObject.myFunction();

Is there some inherent thing im missing about references of vectors? Why does a pointer to a vector of pointers work but a reference to a vector of objects doesnt? Shouldn’t the passed vector be an alias to the original vector? Why is the reference showing changes but not the object it referenced?

Why wont my changes stick unless i used a vector of pointers instead of a vector of objects.

  • 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-23T07:10:59+00:00Added an answer on May 23, 2026 at 7:10 am

    Hold on a minute. When you changed this snippet:

    (*myVector)[i]->myFunction()
    

    To this:

    myVector[i].myFunction()
    

    You’ve actually changed two things:

    1. myVector went from pointer to reference;
    2. The type of the elements inside myVector went from pointer to simple value type.

    If you changed the type of the elements to a simple value type (as in std::vector<int*> to std::vector<int>), then when you add them to your vector, they are copied to it. (The way the vector is passed around, that is, by pointer or by reference, doesn’t affect this behavior. The way the vector is passed and the way the objects inside the vector are passed are completely independent.) This mean that whenever you make a change to them, you only change the version that’s inside the vector.

    There’s nothing wrong with using a vector that holds pointers (as long as you do your memory management in a sensible way). If you want the changes to the version you have in your vector to be replicated to the originals, you’ll have to use references or pointers. Since it’s a world of pain to have a vector contain references (as in std::vector<int&>), if at all possible, you would probably prefer pointers.

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

Sidebar

Related Questions

i'm trying to set up the selenium-maven-plugin and having some difficulties i hope somebody
I'm having some difficulties with Ajax.BeginForm I have something like this in a view
I'm having some difficulties parsing strings of DateTime using DateTime.ParseExact. DateTime result; CultureInfo provider
I'm having some difficulties creating a javascript appointment style calendar. While it does render,
I've been having some difficulties with setting the height of a listbox. Code like
I'm having some difficulties to sort my results by Date. Is there any special
I'm having some difficulties while trying to understand the whole zend routing concept. My
I am having some difficulties in making a form submission via a boxcomponent. I
I’m having some difficulties while trying to consume an unmanaged-code dll from my application
I'm trying to replicate some C# code in PHP5 and am having some difficulties.

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.