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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:22:58+00:00 2026-05-25T12:22:58+00:00

I am reading some C++ text at the address: https://cs.senecac.on.ca/~chris.szalwinski/archives/oop244.071/content/custo_p.html . In the section

  • 0

I am reading some C++ text at the address: https://cs.senecac.on.ca/~chris.szalwinski/archives/oop244.071/content/custo_p.html.
In the section RETURNING A REFERENCE, the author wrote:

”
Returning a reference from a function has two intrinsic benefits:

  • efficiency

  • creates an lvalue

“

I can understand the first benefit (efficiency), but do not understand why is the second one.

As the author explained, I can understand that what is lvalue, but I do not understand what is the relationship between the lvalue and returning a reference from a function so that lvalue becomes a benefit?

  • 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-25T12:22:59+00:00Added an answer on May 25, 2026 at 12:22 pm

    That simply means that you can assign something to the result of the function. Consider this example:

    int& foo() {
      static int x;
      return x;
    }
    
    void bar() {
      foo() = 42;
    }
    

    Instead of int, this works with class objects as well, of course. The point is that by returning a reference to x the caller can directly assign the locally scoped variable in foo. This is what the author means by saying that a reference return value "creates an lvalue".

    By the way, the example actually has an application, as it solves the static initialisation order fiasco for more complex types than int.

    Edit

    As for the "benefit", there are several ways to interpret this. Either it is a way of making your code more readable (as opposed to the same functionality implemented with pointers) or that you can use operations that can only be applied to an lvalue, which would not be possible if you would not return a reference/pointer but received an argument:

    struct X {
      int a;
      int b;
    };
    class Y_withoutRefs {
      private:
        X x;
      public:
        void setXa(int a) {
          x.a = a;
        }
        int getXa() {
          return x.a;
        }
        void setXb(int b) {
          x.b = b;
        }
        int getXb() {
          return x.b;
        }
    };
    class Y_withRefs {
      private:
        X x;
      public:
        X& getX() {
          return x;
        }
    };
    
    void dosmth() {
      Y_withoutRefs ywo;
      Y_withRefs yw;
      ywo.setXa(50+ywo.getXa());
      yw.getX() += 50;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading some C++ text from the address https://cs.senecac.on.ca/~chris.szalwinski/archives/btp200.081/content/overl.html . in the first
I am reading some C text at the address https://cs.senecac.on.ca/~btp100/pages/content/const.html . In the section
I am reading some C text at the address: https://cs.senecac.on.ca/~btp100/pages/content/files.html in the section OPENING
I am reading some C text at the address: https://cs.senecac.on.ca/~lczegel/BTP100/pages/content/compu.html In the section: Addressible
I am reading some text about the C language at the url https://cs.senecac.on.ca/~btp100/pages/content/compu.html .
I am reading a text file with this format: grrr,some text,45.4321,54.22134 I just have
I've trying to get some vertical text on a little test webpage after reading
I have some code reading a text file and scanning for words between brackets:
I am reading a text file which I guess is encoded in utf-8. Some
Reading some Python (PyQt) code, I came across as follows. @pyqtSignature(QString) def on_findLineEdit_textEdited(self, text):

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.