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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:23:54+00:00 2026-05-14T05:23:54+00:00

Please take a look at this example: #include <iostream> #include <vector> #include <string> using

  • 0

Please take a look at this example:

#include <iostream>
#include <vector>
#include <string>
using namespace std;

class mySubContainer
{
public:
    string val;
};

class myMainContainer
{
public:
    mySubContainer sub;
};

void doSomethingWith( myMainContainer &container )
{
    container.sub.val = "I was modified";
}

int main( )
{
    vector<myMainContainer> vec;


    /**
     * Add test data
     */
    myMainContainer tempInst;

    tempInst.sub.val = "foo";
    vec.push_back( tempInst );

    tempInst.sub.val = "bar";
    vec.push_back( tempInst );


    // 1000 lines of random code here

    int i;
    int size = vec.size( );
    myMainContainer current;

    for( i = 0; i < size; i ++ )
    {
        cout << i << ": Value before='" << vec.at( i ).sub.val << "'" << endl;

        current = vec.at( i );
        doSomethingWith( current );

        cout << i << ": Value after='" << vec.at( i ).sub.val << "'" << endl;
    }
    system("pause");//i suck

}

A hell lot of code for an example, I know.

Now so you don’t have to spend years thinking about what this [should] do[es]: I have a class myMainContainer which has as its only member an instance of mySubContainer. mySubContainer only has a string val as member.

So I create a vector and fill it with some sample data.

Now, what I want to do is: Iterate through the vector and make a separate function able to modify the current myMainContainer in the vector. However, the vector remains unchanged as the output tells:

0: Value before='foo'
0: Value after='foo'
1: Value before='bar'
1: Value after='bar'
  • What am I doing wrong?

doSomethingWith has to return void, I can’t let it return the modified myMainContainer and then just overwrite it in the vector, that’s why I tried to pass it by reference as seen in the doSomethingWith definition above.

  • 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-14T05:23:55+00:00Added an answer on May 14, 2026 at 5:23 am

    You’re making a copy of the vector here:

    current = vec.at( i );
    

    and modifying current, but printing the original, vec.at(i).

    Instead, modify the object directly, e.g.

    doSomethingWith(vec[i]);  // or vec.at(i) for checked access.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 494k
  • Answers 494k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You have a few options one is to define the… May 16, 2026 at 10:59 am
  • Editorial Team
    Editorial Team added an answer #include <iostream> // ... Complex s(3.45,23.12); std::cout << s; See… May 16, 2026 at 10:59 am
  • Editorial Team
    Editorial Team added an answer If you are just manually adding an existing RESX and… May 16, 2026 at 10:59 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Before I ask my question, please take a look at this example function: DateTime.TryParse(01/01/2000,
Please take a look at this spider example in Scrapy documentation. The explanation is:
Please take a look at this site as a reference: http://www.philsalesses.com/place-pulse/ On the left
Can someone please explain to me what is the difference between protected / public
I'm going crazy with this my little app... Please help me!!! this is the
This is my first question on SO, so if it seems beyond dumb, please
I haven't really done much on this side of project development so please forgive
my jQuery skills are pretty good normally but this is driving me mad! It's
I am curious about the impact my typedef approach has on my builds. Please
I have tried to ask a variant of this question before. I got some

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.