C++ 之间的区别const 引用和 const?

发布于 2024-09-27 07:57:10 字数 173 浏览 9 评论 0原文

和(无&符号)之间有什么区别

const double& pi = 3.14;

const double pi = 3.14;

它们似乎都具有相同的 L 和 R 值,那么有什么区别呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

末蓝 2024-10-04 07:57:10

对于您的特定示例,没有区别。

这意味着无论如何都无法区分它们。

然而,由于第一个绑定了对临时对象的引用,因此当类型是类类型时,临时对象可以是派生类,例如由函数生成!然后它在作用域末尾正确调用其析构函数。这个小技巧在 ScopeGuard 实现中使用(请参阅原始 DDJ 中的 ScopeGuard 文章,由 Petru Marginean 和Andrei Alexandrescu——Petru 发明了 ScopeGuard,Andrei 在上面做了一个更通用的东西)。

我曾经问 C++ 语言的创建者 Bjarne Stroustrup,为什么支持第一个声明中的语法。

他的答复是,主要是有统一的规则(即不对本地引用做出任何特殊例外,而不是作为形式参数的引用)。我想当时我们都不熟悉 ScopeGuard。回想起来很简单,但需要佩特鲁或安德烈这样的头脑才能想出这样的东西! :-)

干杯&嗯。

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.

罪#恶を代价 2024-10-04 07:57:10

与引用的重要区别在于引用本身是继承常量。一旦引用本身最初被分配给一个变量,它就不能再引用另一个变量。所有修改它的尝试都会修改它引用的变量。鉴于此,const 将意味着该引用是对 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.

您还可以测试关于引用本身恒定的理论,如下所示:

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

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.
悟红尘 2024-10-04 07:57:10

关于常量引用、double 的引用和常量的一些说明。

引用 引用

引用现有的对象,并且不能重新定位。也就是说,一旦声明(定义)引用,它将始终引用该项目。

常量引用

C++ 语言允许声明常量引用。这告诉编译器该引用不会改变。这可能是多余的,因为无法重新放置引用。然而,语言语法允许这样做。

常量

常量是一个值,引用任何东西。

优化与替换

允许编译器用相应的对象、常量或文字替换(替换)对对象、常量或文字的引用,前提是编译器可以保证在其使用范围内不会对该对象执行写入操作当引用传递给该范围内的方法或函数时,此确定可能会变得困难。

为引用指定 const 修饰符将使编译器的工作更容易优化。常量引用是与程序员和用户之间的契约,即引用不会改变。

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.

意中人 2024-10-04 07:57:10

常量双精度数&一个是对常量双精度型的引用,另一个是常量双精度型。引用是一种 const 指针,永远不会改变的指针。

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.

深白境迁sunset 2024-10-04 07:57:10

在 C++ 中,引用本质上是 const。一旦指定,您就无法更改它们。它们必须被声明和初始化。

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

国粹 2024-10-04 07:57:10

引用不是 const 只是值是 const,所以您应该能够重新分配引用,这意味着以下内容是可以的:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文