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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:52:23+00:00 2026-05-20T00:52:23+00:00

In GMan’s answer here , the destructor of the restore_base class isn’t virtual ,

  • 0

In GMan’s answer here, the destructor of the restore_base class isn’t virtual, so I keep wondering how exactly that works. Normally you’d expect the destructor of restorer_base to be executed only, after the object goes out of scope, but it seems that the derived restorer_holder destructor is really called. Anyone care to enlighten me?

  • 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-20T00:52:24+00:00Added an answer on May 20, 2026 at 12:52 am

    The standard case where you need a virtual destructor is

    void foo()
    {
       scoped_ptr<Base> obj = factory_returns_a_Derived();
    
       // ... use 'obj' here ...
    }
    

    And the standard case where you don’t is

    void foo()
    {
       Derived obj;
    
       // ... use 'obj' here ...
    }
    

    GMan’s code is doing something a little trickier, that turns out to be equivalent to the second case:

    void foo()
    {
       Base& obj = Derived();
    
       // ... use 'obj' here ...
    }
    

    obj is a bare reference; normally, it would not trigger destructors at all. But it’s initialized from an anonymous temporary object whose static type — known to the compiler — is Derived. When that object‘s lifetime ends, the compiler will call the Derived destructor. Normally an anonymous temporary object dies at the end of the expression that created it, but there’s a special case for temporaries initializing a reference: they live till the reference itself dies, which here is the end of the scope. So you get pseudo-scoped_ptr behavior and you don’t need a virtual destructor.

    EDIT: Since this has now come up twice: The reference does not have to be const for this special rule to apply. C+98 [class.temporary]/5:

    The second context [in which a temporary object is not destroyed at the end of the
    full-expression] 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 …

    Emphasis mine. There is no mention of const in this language, so the reference does not have to be const.

    EDIT 2: Other rules in the standard prohibit creation of non-const references to temporary objects that are not lvalues. I suspect that at least some temporary objects are lvalues, but I don’t know for certain. Regardless, that does not affect this rule. It would still be formally true that non-const references to temporary objects prolong their lifetime even if no strictly conforming C++ program could ever create such a reference. This might seem ridiculous, but you’re supposed to read standardese this literally and pedantically. Every word counts, every word that isn’t there counts.

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

Sidebar

Related Questions

I just read a comment by GMan that class A { public: A() :
I saw that @GMan implemented a version of sizeof... for variadic templates which (as
GMan has posted a code for the delicious auto_cast operator that allows to write
For example BOOST_PP_ITERATE and BOOST_PP_ITERATION , as seen on GMan's answere here , are
I was coding up a C++ class today, and I wrote a function that
Given GMan's deliciously evil auto_cast utility function concocted here , I've been trying to
While playing with this answer by user GMan I crafted the following snippet (compiled
I`m working on an app that does reading and handling specific URIs from NFC
A recent talk about unordered_map in C++ made me realize that I should use
I am trying to access a C++ class and call its method from a

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.