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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:41:04+00:00 2026-05-14T04:41:04+00:00

If you return a value (not a reference) from the function, then bind it

  • 0

“If you return a value (not a reference) from the function, then bind it to a const reference in the calling function, its lifetime would be extended to the scope of the calling function.”

So: CASE A

const BoundingBox Player::GetBoundingBox(void)
{
    return BoundingBox( &GetBoundingSphere() );
}

Returns a value of type const BoundingBox from function GetBoundingBox()

variant I: (Bind it to a const reference)

const BoundingBox& l_Bbox = l_pPlayer->GetBoundingBox();

variant II: (Bind it to a const copy)

const BoundingBox l_Bbox = l_pPlayer->GetBoundingBox();

Both work fine and I don’t see the l_Bbox object going out of scope. (Though, I understand in variant one, the copy constructor is not called and thus is slightly better than variant II).

Also, for comparison, I made the following changes.

CASE B

BoundingBox Player::GetBoundingBox(void)
{
    return BoundingBox( &GetBoundingSphere() );
}

with Variants:
I

BoundingBox& l_Bbox = l_pPlayer->GetBoundingBox();

and II:

BoundingBox l_Bbox = l_pPlayer->GetBoundingBox();

The object l_Bbox still does not go out scope. How does “bind it to a const reference in the calling function, its lifetime would be extended to the scope of the calling function”, really extend the lifetime of the object to the scope of the calling function ?

Am I missing something trivial here?

  • 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-14T04:41:04+00:00Added an answer on May 14, 2026 at 4:41 am

    Normally a temporary object (such as one returned by a function call) has a lifetime that extends to the end of the “enclosing expression”. However, a temporary bound to a reference generally has it’s lifetime ‘promoted’ to the lifetime of the reference (which may or may not be the lifetime of the calling function), but there are a couple exceptions. This is covered by the standard in 12.2/5 “Temporary objects”:

    The temporary to which the reference is bound or the temporary that is the complete object to a subobject of which the temporary is bound persists for the lifetime of the reference except as specified below. A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the constructor exits. A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full expression containing the call.

    See the following for more information:

    • C++ constant reference lifetime (container adaptor)
    • GotW #88: A Candidate For the “Most Important const”

    An example that might help visualize what’s going on:

    #include <iostream>
    #include <string>
    
    class foo {
    public:
        foo( std::string const& n) : name(n) { 
            std::cout << "foo ctor - " << name + " created\n"; 
        };
        foo( foo const& other) : name( other.name + " copy") { 
            std::cout << "foo copy ctor - " << name + " created\n";
        };
    
        ~foo() { 
            std::cout << name + " destroyed\n"; 
        };
    
        std::string getname() const { return name; };
        foo getcopy() const { return foo( *this); };
    
    private:
        std::string name;
    };
    
    std::ostream& operator<<( std::ostream& strm, foo const& f) {
        strm << f.getname();
        return strm;
    }
    
    
    int main()
    {
        foo x( "x");
    
        std::cout << x.getcopy() << std::endl;
    
        std::cout << "note that the temp has already been destroyed\n\n\n";
    
        foo const& ref( x.getcopy());
    
        std::cout << ref << std::endl;
    
        std::cout << "the temp won't be deleted until after this...\n\n";
        std::cout << "note that the temp has *not* been destroyed yet...\n\n";
    }
    

    Which displays:

    foo ctor - x created
    foo copy ctor - x copy created
    x copy
    x copy destroyed
    note that the temp has already been destroyed
    
    
    foo copy ctor - x copy created
    x copy
    the temp won't be deleted until after this...
    
    note that the temp has *not* been destroyed yet...
    
    x copy destroyed
    x destroyed
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 448k
  • Answers 448k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try weak linking the MapKit framework. May 15, 2026 at 8:01 pm
  • Editorial Team
    Editorial Team added an answer I finally figured this one out: <DataTemplate x:Key="editor"> <ContentPresenter Content="{Binding… May 15, 2026 at 8:01 pm
  • Editorial Team
    Editorial Team added an answer It cannot convert WeatherStation to int because WeatherStation is a… May 15, 2026 at 8:01 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.