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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:24:07+00:00 2026-05-24T04:24:07+00:00

I feel like this question is basic enough to be out there somewhere, but

  • 0

I feel like this question is basic enough to be out there somewhere, but I can’t seem to be able to find an answer for it.

Suppose I have this code:

//class member function
std::map< std::string, std::string > myMap;

const std::map< std::string, std::string >& bar()
{
   return myMap;
}

void myFunc( std::map< std::string, std::string >& foo1 )
{
   foo1 = bar();
   std::map< std::string, std::string >& foo2 = bar();
}

My understanding is that if I start using foo2, since foo2 is a reference to the same instance as what bar() returns, anything I do with foo2 will be reflected in myMap. But what about foo1? Does foo1 get a copy of myMap or does it also point to the same instance as what bar() returns? The c++ standard library says that the assignment operator for std::map will copy the elements over, but then does that mean the assignment operator is not really invoked in the declaration of foo2?

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-05-24T04:24:08+00:00Added an answer on May 24, 2026 at 4:24 am

    References are not reseatable in C++. This means that once they’re initialized, you can’t reassign them. Instead, any assignment actually involve the referred object. So in your code,

    foo1 = bar();
    std::map< std::string, std::string >& foo2 = bar();
    

    the first line calls std::map::operator= on the object that was passed as a parameter to myFunc. After that, foo1 stills refers to that same object — but its value (e.g. what elements it holds) may very well has been changed.

    Note that the second line is not assignment if there ever was any doubt that it was in your mind. Instead, it is an initialization. Since the return type of bar is actually std::map<std::string, std::string> const&, it can’t bind to std::map<std::string, std::string>& so it’s a compile error.


    To expand on the ‘philosophical’ side of things, C++ references are designed to be as transparent as possible and don’t really exist as objects. This is using the C++ Standard meaning of the term (it is not related to OOP): it means that for instance reference types do not have a size. Instead, sizeof(T&) == sizeof(T). Similarly, references do not have addresses and it’s not possible to form a pointer or a reference to a reference: given int& ref = i;, then &ref == &i.

    References are thus purposefully meant to be used as if the referred objects were being used themselves. The only thing reference-specific that happens in the lifetime of a reference is its initialization: what it can bind to and what it means in terms of lifetime.

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

Sidebar

Related Questions

I feel like this is a very basic CSS question: There seems to be
I feel like this is a dumb question and I'm missing something, but I
I feel like I should know this, but I haven't been able to figure
I feel like this is a stupid question because it seems like common sense
I'm sorry, but this is beginning to feel like kicking myself in the head.
I feel like I'm re-inventing the wheel here, but I need to find a
I feel like a fool, but here goes: public interface IHasErrorController{ ErrorController ErrorController {
I feel like developers talk about memory leaks but when you ask them what
I feel like i am completely missing something because I am not able to
... which I didn't feel like splitting into several question posts, since I guess,

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.