开发者

Difference between C++ const references and consts?

开发者 https://www.devze.com 2023-01-20 08:25 出处:网络
What is the difference between: const double& pi 开发者_运维问答= 3.14; and (no ampersand): const double pi = 3.14;

What is the difference between:

const double& pi 开发者_运维问答= 3.14;

and (no ampersand):

const double pi = 3.14;

They both seem to have the same L and R values so what is the difference?


For your particular example there's no difference.

And that means, no way to tell them apart, whatsoever.

However, since the first binds a reference to a temporary, when the type is of class type the temporary can be of a derived class, e.g. produced by a function! And it then has its destructor properly called at the end of the scope. This little el neato trick is used in ScopeGuard implementations (see the original ScopeGuard article in DDJ, by Petru Marginean and Andrei Alexandrescu -- Petru invented ScopeGuard and Andrei made a more general thing on top).

I once asked Bjarne Stroustrup, who created the C++ language, why the syntax in your first declaration is supported.

And his reply was that it was mostly to have uniform rules (i.e. to not make any special exception for local references as opposed to references as formal parameters). I think at that time neither of us were familiar with ScopeGuard. It's simple in retrospect, but it takes a mind like Petru's, or Andrei's, to come up with something like that! :-)

Cheers & hth.


The important difference with a reference is that a reference itself is inheritly constant. Once the reference itself has been initially assigned to a variable, it can not then reference another variable. All attempts to modify it will modify the variable it refers to. Given this, the const will mean that the reference is a reference to a const int.

const int A;
const int B;
const int& Reference = A;
Reference = B; // Error, the value of A can not be assigned, nor would this *ever* be able to make Reference refer to B.

You can also test this theory about a reference itself being constant like so:

const int& const Reference; // Should give a warning about the second const being redundant.


A bit of clarification about constant references, references and constants for doubles.

Reference

A reference refers to an existing an object and cannot be reseated. That is, once you declare (define) the reference, it will always refer to that item.

Constant Reference

The C++ language allows for declaring of a constant reference. This tells the compiler that the reference will not change. This may be redundant since references cannot be reseated. However, the language syntax allows it.

Constant

A constant is a value, and does not refer to anything.

Optimizations & Substitutions

The compiler is allowed to substitute (replace) a reference to an object, constant or literal with the corresponding object, constant or literal, provided that the compiler can guarantee that no write operations are performed to that object within the scope it is used in. This determination may become difficult when the reference is passed to methods or functions within that scope.

Specifying the const modifier to a reference will make the compiler's job easier for optimizing. The constant reference is a contract with the programmer and user that the reference will not be changed.


const double& is a reference to a constant double, the other one is a constant double. A reference is kind of a const pointer, a pointer that never changes.


In C++ the references are inherently const. Once they have been assigned you can not changes them. They must be both declared and initialized.


The reference isnt const only the value is const, so you should be able reassign referense, that means the following would be ok:

const double& pi = 3.14;
const double  pi2 = 2.78;
pi = *(&pi2);
0

精彩评论

暂无评论...
验证码 换一张
取 消