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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:44:38+00:00 2026-06-01T20:44:38+00:00

So from a question asked in another thread, I have thought of a new

  • 0

So from a question asked in another thread, I have thought of a new question and the answer is not obvious to me.

So it appears there is a c++ rule that says if you have a const reference to a temporary, then the lifetime of the temporary is at least as long as the const reference. But what if you have a local const reference to another object’s member variable and then when you leave scope – Does it call the destructor of that variable?

So here is modified program from the original question:

#include <iostream>
#include <string>
using namespace std;

class A {
public:
   A(std::string l) { k = l; };
   std::string get() const { return k; };
   std::string k;
};

class B {
public:
   B(A a) : a(a) {}
   void b() { cout << a.get(); }  //Has a member function
   A a;
};

void f(const A& a)
{  //Gets a reference to the member function creates  a const reference
     stores it and goes out of scope
 const A& temp = a;
 cout << "Within f(): " << temp.k << "\n";
}

int main() {
   B b(A("hey"));

   cout << "Before f(): " << b.a<< "\n";

   f(b.a);

   cout << "After f(): " << b.a.k << "\n";

   return 0;
}

So when I run this code, I get “hey” as the value everytime. Which seems to imply that a local const reference does not bind itself through life with a passed in member object. Why doesn’t it?

  • 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-01T20:44:40+00:00Added an answer on June 1, 2026 at 8:44 pm

    b.a is not a temporary so its lifetime is not affected by any references that are subsequently bound to it.

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

Sidebar

Related Questions

Following on from another question I have asked I have created a new class
I have another question. Quite similar to the other that i already asked (and
I have asked another question related to this in this thread Where to put
Today i was asked this question about sharing data from a thread t1 that
This was the question asked in interview. Can we call one constructor from another
I asked a question here about an Linq error that results from mixing Linq-To-SQL
Following on from another question I asked, Scala 2.8 breakout , I wanted to
OK, so this is leading on from another question I asked here recently. Basically,
While this question asked something similar too, I'm interested in this from another angle.
I have asked a similar question before here , but after much thought, and

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.