C ++ Const用法说明 [英] C++ Const Usage Explanation

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

问题描述

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-correctness

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

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

const int * const 意味着一个常量指针指向一个常量int:ie一个不能被改变的指针,一个不能改变的int:this和<$ cc之间的唯一区别$ c> 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& 意味着对常量指针的引用。通常指针不通过引用传递; 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 ++ Const用法说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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