C++ 之 const references

extraction from The C++ Programming Language 4th. ed., Section 7.7 References, Bjarne Stroustrup

To reflect the lvalue/rvalue and const/non-const distinctions, there are three kinds of references:

  • lvalue references: to refer to objects whose value we want to change
  • const references: to refer to objects whose value we do not want to change (e.g., a constant)
  • rvalue references: to refer to objects whose value we do not need to preserve after we have used it (e.g., a temporary)

Collectively, they are called referencs. The first two are both called lvalue references.

 The obvious implementation of a reference is as a (constant) pointer that is dereferenced each time it is used. It doesn‘t do much harm to think about references that way, as long as one remembers that a reference isn‘t an object that can be manipulated the way a pointer is.

 In some cases, the compiler can optimize away a reference so that there is no object representing that reference at run time.

 Intialization of a reference is trivial when the initializer is an lvalue (an object whose address you can take). The initializer for a "plain" T& must be an lvaue of type T.

 The intializer for a const T& need not be an lvaue or even of type T. In such cases:

  1. First, implicit type conversion to T is applied if necessary.
  2. Then, the resulting value is placed in a temporary variable of type T.
  3. Finally, this temporary variable is used as the value of the initializer.

Consider:

  double &dr=1;      //error: lvalue needed

  const double& cdr{1};  //OK

The iterpretation of this last initialization might be:

  double temp = double{1};

  cosnt double &cdr {temp};

A temporary created to hold a reference initializer persists until the end of its reference‘s scope.

References to variables and references to constants are distinguished because introducing a temporary for a variable would have been highly error-prone; an assignment to the variable would become an assignment to the -- soon-to-disappear -- temporary. No such problem exists for references to constants, and references to constants are often important as function arguments.

时间: 2024-10-07 09:02:09

C++ 之 const references的相关文章

非const引用不能指向临时变量

没找到具体原因,MSDN看到下面这句,VC是从2008才有这一限制的,感觉就是从语法上对临时变量增加了限定,因为一般说来修改一个临时变量是毫无意义的,通过增加限定,强调临时变量只读语义.虽然实际上修改临时变量并不会有问题. Visual Studio 2008 In previous releases of Visual C++, non-const references could be bound to temporary objects. Now, temporary objects ca

Constructors for mutable and const versions of an iterator class

According to section 3.2 of the book "Generic Programming and STL", it is recommended to define both mutable and const versions of an iterator class. However, the constructors provided in the example code are still not perfect because some cases

References & the Copy-Constructor

1 There are certain rules when using references: (Page 451) A reference must be initialized when it is created. (Pointers can be initialized at any time.) Once a reference is initialized to an object, it cannot be changed to refer to another object.

Understand the Qt containers(有对应表)

Container classes are one of the cornerstones of object-oriented programming, invaluable tools that free us from having to permanently think about memory management. Qt comes with its own set of container classes, closely modeled after those in the S

Google C++ 代码规范

Google C++ Style Guide Table of Contents Header Files Self-contained Headers The #define Guard Forward Declarations Inline Functions Names and Order of Includes Scoping Namespaces Unnamed Namespaces and Static Variables Nonmember, Static Member, and

Google C++ Style----头文件

转载请注明出处<http://blog.csdn.net/qianqin_2014/article/details/51354434> 一.头文件 通常,每一个.cc 文件(C++的源文件)都有一个对应的.h 文件(头文件),也有一些例 外,如单元测试代码和只包含 main()的.cc 文件. 正确使用头文件可令代码在可读性.文件大小和性能上大为改观. 下面的规则将引导你规避使用头文件时的各种麻烦. 1. #define 的保护 所有头文件都应该使用#define 防止头文件被多重包含(mul

Learn X in Y minutes Where X=c++

http://learnxinyminutes.com/docs/c++/ C++ is a systems programming language that, according to its inventor Bjarne Stroustrup, was designed to be a “better C” support data abstraction support object-oriented programming support generic programming Th

Google C++ Style Guide----英文版

转载请注明出处<http://blog.csdn.net/qianqin_2014/article/details/51354326> Background C++ is the main development language used by many of Google's open-source projects. As every C++ programmer knows, the language has many powerful features, but this power

C/C++编程规范——头文件

在选择编程规范时,我首选google,其次是华为与微软,最后根据自身的一些实际情况进行调整.以下内容摘自google的c/c++编程规范.--------------------------通常,每一个.cc 文件(C++的源文件)都有一个对应的.h 文件(头文件),也有一些例外,如单元测试代码和只包含 main()的.cc 文件.正确使用头文件可令代码在可读性.文件大小和性能上大为改观.下面的规则将引导你规避使用头文件时的各种麻烦. 1. #define 的保护所有头文件都应该使用#defin