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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:20:20+00:00 2026-05-23T04:20:20+00:00

My question is when does any one need to return an object by reference?

  • 0

My question is when does any one need to return an object by reference? Well this comes from the fact that we can as well pass the object to be filled through the parameter list itself. Are there any particular scenarios where it mandates the return by reference.

I’m asking this with respect to non member functions.

Edit: I’m aware of its usage in operator overloading.

  • 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-23T04:20:21+00:00Added an answer on May 23, 2026 at 4:20 am

    You need to return as reference when you want the reveiving end to have access to the referred object. Consider operator[] in a map and the use case:

    std::map<std::string, int> word_count;
    std::string word;
    while ( std::cin >> word ) {
       word_count[ word ]++;
    }
    

    You do not want to extract the value from the map, but rather access the stored object in this case to modify it. The same goes with many other designs, where you need access to some internal data, but you do not want to copy it:

    if ( person.name() == "Pete" ) {
    

    The user does not need to copy the object, only to check whether the object has a concrete value. You could have forced a copy by returning by value, and the semantics would be the same, but with higher costs. Or you could create a local variable and pass it by reference to a function that will fill it, in which case you are not only incurring the cost of copying, but also making code more cumbersome:

    std::string name;
    person.fill_name( name );
    if ( name == "Pete" ) {
    

    Which, as you can probably notice, is much more cumbersome in all uses of the member function.

    Now, I see the “I’m asking this with non-member functions”, well, the same rationale applies at different levels. Free standing functions can be applied to objects, consider:

    boost::any any = 5;
    boost::any_cast<int&>( any )++;
    

    The function any_cast is a free function and yet it returns a reference to another instance. If you need access to the actual object and not a copy, then a reference is the solution. Note that for only reading you don’t need a reference and you may as well return by value:

    std::cout << boost::any_cast<int>( any );   // will print 6 now
    

    Similarly in all cases where the function returns references to objects that are not arguments, but globals or static (returning a reference to a variable with auto storage in a function is undefined behavior, but in all cases where it is correct to do so the semantics would not be the same if you change it with any other solution.

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

Sidebar

Related Questions

I need to return data from two tables - one table contains records on
I understand that only one instance of any object according to .equals() is allowed
Maths101 question - does anyone know how to calculate an ellipse (width/height) that will
Does anyone know? And a bigger question is what happens when you encounter this
Does anyone know if Python's shelve module uses memory-mapped IO? Maybe that question is
Can anyone explain me the difference of all and also an another question does
This question is related to my previous question Link The method bellow does excatly
I need help in understanding this interview question: Q: Find an algorithm to find
This is actually a much more concise, much more clear question than the one
[Question] Does anyone product release notes via an automated process? If so how. Especially

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.