C++ 常量使用说明 [英] C++ Const Usage Explanation

查看:15
本文介绍了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++ 常量使用说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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