Eri*_*ric 7 c++ syntax reference
我知道通过引用传递和传递指针的主题被大量覆盖...我很清楚我理解所有的细微差别,直到我读到这个:
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!
Run Code Online (Sandbox Code Playgroud)
作者的意思是"注意我把const放在TYPE的右边,因为我们不知道TYPE ......是否是一个指针!"
我在这个主题上读过的所有内容都一致地说:
void foodbar(TYPE const&)
也是等同的
void foobar(const TYPE&)
如果我理解正确的作者,他/她说的是:
const int*X vs int*const X其中指针,X本身是const而X指向的是const?
如果是这样,这是真的吗?
如果TYPE是类似#define的东西int*,那么放置const确实很重要.在这种情况下,您将获得const int*或int* const取决于的位置const.
如果TYPE是typedef或模板参数,则 const无论哪种方式都会影响整个类型.
对我来说,这看起来更像是针对宏的另一个论点,而不是声明中某些特定样式的需要.