c++ - 复制ctor和复制赋值运算符const关键字位置

标签 c++

我最近注意到一些代码示例中的 const 关键字在中间移动(来自受人尊敬的博客网站),像这样:

X(X const& that) { .../... }

X& operator=(X const& other) { .../... }

这等同于这种更熟悉的语法吗?

X(const X& that) { .../... }

X& operator=(const X& other) { .../... }

那样切换“const”的目的是什么?

最佳答案

1) const放在哪里并不重要:const int & == int const &

2)为什么?是助记规则。

int * const - 常量指针(const after *)

int const * - 指向常量值的指针(const after int)。

所以,这是为了保持一致性。

关于c++ - 复制ctor和复制赋值运算符const关键字位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34835774/

相关文章:

c++ - 在 STL vector 中管理作用域和对象生命周期

c++ - "Undefined reference to"

c++ - 如何为要在 STL 数据结构中使用的任意结构实现运算符 <?

c++ - 实现 partition_unique 和 stable_partition_unique 算法

c++ - 关于 Qt MessageBox::warning() 重载

C++:const static DEBUG 和 if 语句,实际的执行时间开销是多少?

c++ - 为什么重载 += 永远不会在其他人的代码中返回任何内容?

c++ - 启动服务时报错5

c++ - 计算文本文件中整数个数的函数?

c++ - winmain() 函数/C++