GEN*_*NER 3 c++ pointers const
好吧,我没有意识到这const可能像指针一样令人困惑.有人可以在步骤中解释下面的代码到底做了什么const?
const int*const Method3(const int*const&)const;
Run Code Online (Sandbox Code Playgroud)
即使对于非新手程序员来说也是如此令人困惑.
它可能令人困惑,因为它混合了两种风格const.
const int*const Method3(const int*const&)const;
Run Code Online (Sandbox Code Playgroud)
我会对它们进行重新排序,因为在我看来,理解这些的最好方法是向后阅读它们.
让我们从返回类型开始:
const int*const -> int const* const
Run Code Online (Sandbox Code Playgroud)
通过向后阅读它我们得到:const指向const int.
同样,对于函数参数:
const int* const& -> int const* const&
Run Code Online (Sandbox Code Playgroud)
通过向后阅读它我们得到:引用const指针const int.
该函数也标记为const,这意味着它是一个成员函数,例如,当对该类的引用是常量时可以调用它.
有关可能的const优化和进一步理解,请参阅以下答案:
| 归档时间: |
|
| 查看次数: |
107 次 |
| 最近记录: |