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

    <legend id='Lat0H'><style id='Lat0H'><dir id='Lat0H'><q id='Lat0H'></q></dir></style></legend>
    1. <small id='Lat0H'></small><noframes id='Lat0H'>

    2. <tfoot id='Lat0H'></tfoot>
      • <bdo id='Lat0H'></bdo><ul id='Lat0H'></ul>

      引用类型的定义是什么?

      What is definition of reference type?(引用类型的定义是什么?)

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

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

                <bdo id='Q9S1q'></bdo><ul id='Q9S1q'></ul>
                  <tbody id='Q9S1q'></tbody>

              • 本文介绍了引用类型的定义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                你如何以正式和严格的方式定义(解释)什么是 C++ 中的引用类型?

                How do you define (explain) in a formal and strict way what is reference type in C++?

                我试着用谷歌搜索,并查看了 Stroustrup 的C++ 编程语言",但我在那里没有看到这个概念的定义.

                I tried to google, and looked into Stroustrup's "The C++ Programming Language", but I don't see definition of this concept there.

                推荐答案

                引用是一个别名,是对象的替代名称.它本身不是对象(因此不是指针,即使它们的某些用途与指针的用途重叠).

                A reference is an alias, an alternate name for an object. It is not an object itself (and in that way is not a pointer, even if some of their uses overlap with uses of pointers).

                参考文献对其处理有一定的限制,这与它们的非客观性有关.例如,您不能创建引用数组.它们必须在声明后立即进行初始化(绑定、固定),因为如果没有要别名的对象,它们就不可能存在.

                References have certain limitations to their handling, related to their non-objectness. For example, you can't create an array of references. They have to be initialized (bound, seated) as soon as they are declared, since they can't possibly exist without an object to alias.

                然而你可以存储它们,它们遵循自动变量或成员变量的规则.它们的用途之一是查看 C++ 的按值传递函数调用.

                You can however store them, and they obey the rules of automatic variables or member variables. One of their uses is to poke through C++'s pass-by-value function calls.

                请注意,const 引用作为别名有一个巧妙的副作用:当绑定到一个临时(即未命名)对象时,它们会给该对象一个名称,因此将其生命周期延长到引用本身的生命周期.

                Note that const references have a neat side-effect of being aliases : when bound to a temporary (i.e unnamed) object, they give said object a name, and therefore extend its lifetime to that of the reference itself.

                { // Block scope
                     Foo fooVal = makeFoo(); // Say makeFoo() returns a (temporary, unnamed) Foo
                     // Here the temporary Foo is dead (fooVal is a copy).
                
                     // Foo &fooRef = makeFoo(); // Error, reference is non-const
                     Foo const &fooCRef = makeFoo(); // All good
                
                     // ...
                
                     // The second temporary is still alive
                     fooCRef.doSomethingFunny(); // Works like a charm !
                
                } // The second temporary dies with fooRef
                

                但是请注意,有可能(尽管是人为的)对象超出范围并且引用仍然指向它.然后,您将拥有 悬空引用,不再使用它们(这样做将是未定义行为).

                Beware though, it is possible (though contrived) to have an object go out of scope with references still pointing to it. You will then have dangling references, which are not to be used anymore (doing so would be Undefined Behaviour).

                Foo *fooPtr = new Foo; // Here is a Foo
                Foo &fooRef = *fooPtr; // Here is an alias for that Foo
                
                delete fooPtr; // Here is the end of that Foo's life
                
                fooRef.doSomethingFunny(); // Here comes trouble...
                

                这篇关于引用类型的定义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                STL BigInt class implementation(STL BigInt 类实现)
                Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                Move list element to the end in STL(在 STL 中将列表元素移动到末尾)
                Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)

                • <bdo id='ub9xL'></bdo><ul id='ub9xL'></ul>
                  <tfoot id='ub9xL'></tfoot>

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

                        • <legend id='ub9xL'><style id='ub9xL'><dir id='ub9xL'><q id='ub9xL'></q></dir></style></legend>

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