<bdo id='YZZa6'></bdo><ul id='YZZa6'></ul>
  1. <legend id='YZZa6'><style id='YZZa6'><dir id='YZZa6'><q id='YZZa6'></q></dir></style></legend>

  2. <i id='YZZa6'><tr id='YZZa6'><dt id='YZZa6'><q id='YZZa6'><span id='YZZa6'><b id='YZZa6'><form id='YZZa6'><ins id='YZZa6'></ins><ul id='YZZa6'></ul><sub id='YZZa6'></sub></form><legend id='YZZa6'></legend><bdo id='YZZa6'><pre id='YZZa6'><center id='YZZa6'></center></pre></bdo></b><th id='YZZa6'></th></span></q></dt></tr></i><div id='YZZa6'><tfoot id='YZZa6'></tfoot><dl id='YZZa6'><fieldset id='YZZa6'></fieldset></dl></div>

  3. <tfoot id='YZZa6'></tfoot>

      <small id='YZZa6'></small><noframes id='YZZa6'>

      为什么在对基类的 const 引用上调用派生类的析构函数?

      Why is the derived class#39;s destructor invoked on a const reference to the base class?(为什么在对基类的 const 引用上调用派生类的析构函数?)
    1. <small id='4JYKO'></small><noframes id='4JYKO'>

        <tbody id='4JYKO'></tbody>
      <i id='4JYKO'><tr id='4JYKO'><dt id='4JYKO'><q id='4JYKO'><span id='4JYKO'><b id='4JYKO'><form id='4JYKO'><ins id='4JYKO'></ins><ul id='4JYKO'></ul><sub id='4JYKO'></sub></form><legend id='4JYKO'></legend><bdo id='4JYKO'><pre id='4JYKO'><center id='4JYKO'></center></pre></bdo></b><th id='4JYKO'></th></span></q></dt></tr></i><div id='4JYKO'><tfoot id='4JYKO'></tfoot><dl id='4JYKO'><fieldset id='4JYKO'></fieldset></dl></div>
        <tfoot id='4JYKO'></tfoot>
        <legend id='4JYKO'><style id='4JYKO'><dir id='4JYKO'><q id='4JYKO'></q></dir></style></legend>

        • <bdo id='4JYKO'></bdo><ul id='4JYKO'></ul>

              • 本文介绍了为什么在对基类的 const 引用上调用派生类的析构函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在 GMan 的回答中,restore_base 类的析构函数不是 virtual,所以我一直想知道它到底是如何工作的.通常,您希望 restorer_base 的析构函数仅在对象超出范围后才执行,但似乎派生的 restorer_holder 析构函数确实被调用了.有谁愿意教教我吗?

                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?

                推荐答案

                需要虚拟析构函数的标准情况是

                The standard case where you need a virtual destructor is

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

                的标准情况是

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

                GMan 的代码做了一些更棘手的事情,结果与第二种情况等效:

                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 是一个裸引用;通常,它根本不会触发析构函数.但是它是从一个匿名临时对象初始化的,它的静态类型——编译器知道——是Derived.当那个对象的生命周期结束时,编译器将调用Derived析构函数.通常,匿名临时对象在创建它的 表达式 结束时死亡,但临时对象初始化引用有一个特殊情况:它们一直存在到引用本身死亡,这是作用域的结尾.因此,您获得了伪scoped_ptr 行为,并且不需要虚拟析构函数.

                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.

                因为这已经出现了两次:引用不必必须是const才能应用这个特殊规则.C+98 [class.temporary]/5:

                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:

                第二个上下文[其中一个临时对象在结束时不被销毁full-expression] 是 一个引用 绑定到一个临时的.暂时的引用被绑定或作为完整对象的临时对象到子对象临时绑定的对象在引用的生命周期内持续 ...

                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 ...

                强调我的.在这种语言中没有提到 const,所以引用不一定是 const.

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

                EDIT 2: 标准中的其他规则禁止创建对不是左值的临时对象的非常量引用.我怀疑至少一些临时对象左值,但我不确定.无论如何,这不会影响此规则.对临时对象的非常量引用可以延长它们的生命周期,这在形式上仍然是正确的,即使没有严格遵守的 C++ 程序能够创建这样的引用.这可能看起来很荒谬,但您应该从字面上和迂腐地阅读标准语.每一个字都很重要,每一个不存在的字都很重要.

                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.

                这篇关于为什么在对基类的 const 引用上调用派生类的析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                What is a dangling reference?(什么是悬空引用?)
                C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)

                <tfoot id='NlINB'></tfoot>

                      <tbody id='NlINB'></tbody>
                  1. <legend id='NlINB'><style id='NlINB'><dir id='NlINB'><q id='NlINB'></q></dir></style></legend>
                    • <bdo id='NlINB'></bdo><ul id='NlINB'></ul>

                        <small id='NlINB'></small><noframes id='NlINB'>

                        • <i id='NlINB'><tr id='NlINB'><dt id='NlINB'><q id='NlINB'><span id='NlINB'><b id='NlINB'><form id='NlINB'><ins id='NlINB'></ins><ul id='NlINB'></ul><sub id='NlINB'></sub></form><legend id='NlINB'></legend><bdo id='NlINB'><pre id='NlINB'><center id='NlINB'></center></pre></bdo></b><th id='NlINB'></th></span></q></dt></tr></i><div id='NlINB'><tfoot id='NlINB'></tfoot><dl id='NlINB'><fieldset id='NlINB'></fieldset></dl></div>