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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:56:24+00:00 2026-05-30T00:56:24+00:00

This is the simplest I could work my problem down to, sorry about the

  • 0

This is the simplest I could work my problem down to, sorry about the length:

#include <vector>
#include <iostream>

class Bar
{
private:
    std::vector<int> intVector_;
public:
    Bar() {};
    void addInt(int newInt) 
    { 
        intVector_.push_back(newInt); 
        std::cout << intVector_.size() << " "; 
    };
    int getIntVectorSize() { return intVector_.size(); };
};

class Foo
{
private:
    Bar bar_;
public:
    Foo() { bar_ = Bar(); };
    Bar getBar() { return bar_; };
};

int main(char argc, char* argv[])
{
    Foo foo = Foo();
    foo.getBar().addInt(1);
    std::cout << foo.getBar().getIntVectorSize() << " ";
    foo.getBar().addInt(2);
    std::cout << foo.getBar().getIntVectorSize() << " ";
    foo.getBar().addInt(3);
    std::cout << foo.getBar().getIntVectorSize() << " ";
}

My problem is that adding an int to the vector only seems to last for the duration of addInt(). My output for the size of the vector looks like this:

1 0 1 0 1 0

I’m rather new to C++ and all this reference/pointer business, so I am stumped as to how I can fix this, or if this is even possible. Thanks for any help!

  • 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-30T00:56:27+00:00Added an answer on May 30, 2026 at 12:56 am

    It’s because getBar() returns a Bar by value, which makes a copy of bar_ for every call of the function, and you are modifying the vector of the temporary.

    You can avoid this by returning a reference:

    class Foo
    {
    private:
        Bar bar_;
    public:
        Foo() { bar_ = Bar(); }; // you prob. want to use an initialiser list btw.
        Bar& getBar() { return bar_; };
    //     ^ notice the ampersand
    };
    

    This way, any modifications done on the return value of getBar are done on bar_ and not a temporary copy that is destroyed at the end of the statement.

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

Sidebar

Related Questions

This is the simplest example I could come up with that reproduces the problem.
This seems like a very simple and a very common problem. The simplest example
I think this is pretty much the simplest case for mapping a Map (that
I need to reduce this boolean expression to its simplest form. Its given that
My question is about handling temporary files in a small .NET program. This program/utility
I could be the first person to discover this (or at least document it
Hi people I have been at this for like 5 days and could not
I searched around and couldn't find a solution to this problem. I am trying
This apparently doesn't work: redirect_to post_path(@post, :params => params) Since Rails route helpers doesn't
I'm getting this error... ImportError: Could not find 'input_readers' on path 'map reduce' when

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.