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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:52:19+00:00 2026-05-27T10:52:19+00:00

Consider the following code: #include <iostream> struct A { const char *name; A() :

  • 0

Consider the following code:

#include <iostream>

struct A {
    const char *name;

    A() : name("A") {
        std::cout << "A()\n";
    }

    virtual ~A() {
        std::cout << "~A()\n";
    }
};

class B {
    const A& a;
public:

    B() : a(A()) {
    };

    void print_data() const {
        std::cout << a.name << '\n';
    }

    ~B() {
        std::cout << "~B()\n";
    }
};

int main() {
    B b;
    b.print_data();
    return 0;
}

The output from GCC 4.4 is:

A() ~A() A ~B()

This looks strange to me. I would have expected either a copy of the temporary instance of A being bound to the B::a or that temporary itself being destructed during ~B().

Basically, I thought B::a is always a valid reference during the life of b. In fact b.print_data() works apparently and the compiler does not give any warning.

What does the standard c++98/03 say about this matter?

  • 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-27T10:52:19+00:00Added an answer on May 27, 2026 at 10:52 am

    Constant references don’t prolong the life of temporaries in classes, period. It’s just like that. They only do it in Foo const& f = Foo(); where foo returns by-value, but that’s it.

    §12.2 [class.temporary]

    p4 There are two contexts in which temporaries are destroyed at a different point than the end of the full expression. […]

    p5 The second context is when a reference is bound to a temporary. 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.

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

Sidebar

Related Questions

Consider the following code: #include <iostream> #include <memory> #include <vector> using namespace std; struct
Consider this code #include <iostream> #include <cstdio> using namespace std; class Dummy { public:
Consider the following code : #include <vector> #include <iostream> class a { public: int
Consider the following code. #include <iostream> #include <string> struct SimpleStruct { operator std::string ()
Consider the following program: #include <iostream> #include <algorithm> using namespace std; template<class T> struct
Consider the following code: #include <iostream> template<class T> struct outer { struct inner {};
Consider the following code: #include<iostream> using namespace std; class sample { int a; int
Consider the following sample code below: #include <iostream> using namespace std; class base {
please consider following code #include <iostream> using namespace std; class Digit { private: int
Consider the following code: #include <iostream> #include <cstdio> #include <cstring> using namespace std; template<class

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.