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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:34:53+00:00 2026-06-12T13:34:53+00:00

This is a two part question. Is it ok to assign the return value

  • 0

This is a two part question. Is it ok to assign the return value of a function to a reference? Such as

Foo FuncBar()
{
    return Foo();
}

// some where else
Foo &myFoo = FuncBar();

Is this ok? Its my understanding that FuncBar() returns a Foo object and now myFoo is a reference to it.

Second part of the question. Is this an optimization? So if your doing it in a loop a lot of the time is it better to do

Foo &myFoo = FuncBar();

or

Foo myFoo = FuncBar();

And take into account the variables use, won’t using the ref require slower dereferences?

  • 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-06-12T13:34:54+00:00Added an answer on June 12, 2026 at 1:34 pm
    Foo &myFoo = FuncBar();
    

    Will not compile. it should be:

    const Foo &myFoo = FuncBar();
    

    because FuncBar() returns a temporary object (i.e., rvalue) and only lvalues can be bound to references to non-const.

    Is it safe?

    Yes it is safe.

    C++ standard specifies that binding a temporary object to a reference to const lengthens the lifetime of the temporary to the lifetime of the reference itself, and thus avoids what would otherwise be a common dangling-reference error.


    Foo myFoo = FuncBar();      
    

    Is Copy Initialization.
    It creates a copy of the object returned by FuncBar() and then uses that copy to initalize myFoo. myFoo is an separate object after the statement is executed.

    const Foo &myFoo = FuncBar();
    

    Binds the temporary returned by FuncBar() to the reference myFoo, note that myFoo is just an alias to the returned temporary and not a separate object.

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

Sidebar

Related Questions

This is two part question: I have two stored procedures: sp1 & sp2. If
This is really a two part question: I'm seeing some users in the Grantee
This question has two parts. Part 1. Yesterday I had some code which would
This is a two part question; firstly, does anyone out there have some insight
This is a two part question. I am rendering some BSP geometry and some
This is a two part question. The first part is converting the function below
This is a two part question, but first some background information: I have a
This is a two part question. Is it possible to take advantage of xVal
This is a two-part question: First, I am interested to know what the best
This is a two-part question. I'm using jQuery for a project and wanting to

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.