C++ 常量使用说明

C++ Const Usage Explanation(C++ 常量使用说明)
本文介绍了C++ 常量使用说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

const int* const Method3(const int* const&) const;

有人可以解释每个 const 的用法吗?

Can someone explain the usage of each of the const?

推荐答案

阅读:https://isocpp.org/wiki/faq/const-正确性

最后的const表示函数Method3不会修改其类的不可变成员.

The final const means that the function Method3 does not modify the non mutable members of its class.

const int* const 表示指向常量int的常量指针:即不能改变的指针,指向不能改变的int:this和const int&的唯一区别; 是可以null

const int* const means a constant pointer to a constant int: i.e. a pointer that cannot be changed, to an int that cannot be changed: the only difference between this and const int& is that it can be null

const int* const& 表示对指向常量 int 的常量指针的引用.通常指针不是通过引用传递的;const int* & 更有意义,因为这意味着可以在方法调用期间更改指针,这是我可以看到通过引用传递指针 const 的唯一原因int* const& 在所有意图和目的上都与 const int* const 相同,只是它的效率可能较低,因为指针是普通旧数据 (POD) 类型,这些应该在一般按值传递.

const int* const& means a reference to a constant pointer to a constant int. Usually pointers are not passed by reference; const int* & makes more sense because it would mean that the pointer could be changed during the method call, which would be the only reason I can see to pass a pointer by reference, const int* const& is to all intents and purposes the same as const int* const except that it is probably less efficient as pointers are plain old data (POD) types and these should, in general be passed by value.

这篇关于C++ 常量使用说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)