【发布时间】:2023-03-03 17:14:01
【问题描述】:
我知道通过引用传递与通过指针传递的主题被大量讨论......我很确定在我读到这篇文章之前我理解了所有细微差别:
http://carlo17.home.xs4all.nl/cpp/const.qualifier.html
读取(以防链接失效)
The prototype for foobar can have any of the following footprints:
void foobar(TYPE); // Pass by value
void foobar(TYPE&); // Pass by reference
void foobar(TYPE const&); // Pass by const reference
Note that I put the const to the right of TYPE because we don't know if TYPE (this is not a template parameter, but rather for instance a literal char*) is a pointer or not!
作者所说的“注意我把 const 放在 TYPE 的右边是什么意思,因为我们不知道 TYPE ... 是不是指针!”
我读到的关于这个主题的所有内容都一致地说:
void foodbar(TYPE const &)
也是等价的
void foobar(const TYPE &)
如果我对作者的理解正确,他/她的意思是:
const int *X vs int * const X where 指针,X 本身是 const 而 X 指向的是 const?
如果是这样,这是真的吗?
【问题讨论】:
-
作者误解了语言(两者相同),老实说,我不知道他想说什么。还有一个理由是买一本书而不是尝试从一些在线文章中学习。
-
购买一本书并不能保证您拥有优质资源,即使是热门书籍:seebs.net/c/c_tcn4e.html
-
如果你错过了我写的内容,这里又是,“我读过的关于这个主题的所有内容都是一致的。”这包括几本书。特别是 Nicholas A. Solter、Scott J. Kleper 在第 332 页的“Professional C++”中写道:“请记住 const int &zRef 等同于 int const &zRef。” amazon.com/Professional-C-Programmer-Nicholas-Solter/dp/…