为什么使用const非引用,当const引用的生命周期是当前范围的长度 [英] why use a const non-reference when const reference lifetime is the length of the current scope

查看:235
本文介绍了为什么使用const非引用,当const引用的生命周期是当前范围的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在c ++中,如果将函数的返回值赋给const引用,那么该返回值的生命周期将是该引用的作用域。例如

  MyClass GetMyClass()
{
return MyClass(some constructor);
}

void OtherFunction()
{
const MyClass& myClass = GetMyClass(); //返回值的生命周期是直到结束
//由于魔术const引用
的范围的doStuff(myClass);
doMoreStuff(myClass);
} // myClass被销毁

值从一个函数到一个const对象,你可以改为分配给一个const引用。在函数中有没有一种情况,你不想在赋值中使用引用而是使用对象?为什么要写下这行:

  const MyClass myClass = GetMyClass(); 



编辑:我的问题混淆了一对夫妇,所以我添加了GetMyClass函数的定义

编辑2:请不要尝试并回答这个问题,如果你没有看过这个:
http://herbsutter.com/2008/01/01/gotw-88-a -candidate-for-the-most-important-const /

解决方案

如果函数返回一个对象比引用),在调用函数中进行复制是必要的[虽然可以采取优化步骤,这意味着根据as-if原理,将对象直接写入到其中复制将结束的结果存储器中] 。



在示例代码 const MyClass myClass = GetMyClass(); c $ c> myclass ,而不是存在的临时对象,但没有命名(或者看不到,除非你看看机器代码)。换句话说,不管你是否为它声明一个变量,在调用 GetMyClass MyClass c> - 这只是一个问题是否让它可见或不。



Edit2:
const 参考解决方案将会出现类似(不完全相同,解释我的意思,你实际上不能这样做):

  MyClass __noname__ = GetMyClass 
const MyClass& myclass = __noname__;

这只是编译器生成 __ noname __ 变量在幕后,没有真正告诉你。



通过使 const MyClass myclass 对象变得可见,并且清楚发生了什么 GetMyClass 正在返回对象的COPY,而不是对某些已经存在的对象的引用。



另一方面,如果 GetMyClass 确实返回了引用,那么它肯定是正确的事情。



在某些编译器中,使用引用甚至可能在使用对象时添加额外的内存读取,因为引用是指针[是的,标准没有说,但请在抱怨之前,给我一个帮助,并告诉我一个编译器不能实现引用作为指针与额外的糖,以使它们更甜),所以要使用一个参考,编译器应该读引用值(指向对象的指针),然后从该指针读取对象内的值。在非引用的情况下,对象本身对于编译器是已知的作为直接对象,而不是引用,从而节省了额外的读取。当然,大多数编译器会优化这样一个额外的参考,MOST的时间,但它不能总是这样做。


So in c++ if you assign the return value of a function to a const reference then the lifetime of that return value will be the scope of that reference. E.g.

MyClass GetMyClass()
{
    return MyClass("some constructor");
}

void OtherFunction()
{
    const MyClass& myClass = GetMyClass(); // lifetime of return value is until the end            
                                           // of scope due to magic const reference
    doStuff(myClass);
    doMoreStuff(myClass);
}//myClass is destructed

So it seems that wherever you would normally assign the return value from a function to a const object you could instead assign to a const reference. Is there ever a case in a function where you would want to not use a reference in the assignment and instead use a object? Why would you ever want to write the line:

const MyClass myClass = GetMyClass();

Edit: my question has confused a couple people so I have added a definition of the GetMyClass function

Edit 2: please don't try and answer the question if you haven't read this: http://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/

解决方案

If the function returns an object (rather than a reference), making a copy in the calling function is necessary [although optimisation steps may be taken that means that the object is written directly into the resulting storage where the copy would end up, according to the "as-if" principle].

In the sample code const MyClass myClass = GetMyClass(); this "copy" object is named myclass, rather than a temporary object that exists, but isn't named (or visible unless you look at the machine-code). In other words, whether you declare a variable for it, or not, there will be a MyClass object inside the function calling GetMyClass - it's just a matter of whether you make it visible or not.

Edit2: The const reference solution will appear similar (not identical, and this really just written to explain what I mean, you can't actually do this):

 MyClass __noname__ = GetMyClass();
 const MyClass &myclass = __noname__;

It's just that the compiler generates the __noname__ variable behind the scenes, without actually telling you about it.

By making a const MyClass myclass the object is made visible and it's clear what is going on (and that the GetMyClass is returning a COPY of an object, not a reference to some already existing object).

On the other hand, if GetMyClass does indeed return a reference, then it is certainly the correct thing to do.

IN some compilers, using a reference may even add an extra memory read when the object is being used, since the reference "is a pointer" [yes, I know, the standard doesn't say that, but please before complaining, do me a favour and show me a compiler that DOESN'T implement references as pointers with extra sugar to make them taste sweeter], so to use a reference, the compiler should read the reference value (the pointer to the object) and then read the value inside the object from that pointer. In the case of the non-reference, the object itself is "known" to the compiler as a direct object, not a reference, saving that extra read. Sure, most compilers will optimise such an extra reference away MOST of the time, but it can't always do that.

这篇关于为什么使用const非引用,当const引用的生命周期是当前范围的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆