Is the const value parameter in definition but not declaration really C++?

前端 未结 7 1267
时光说笑
时光说笑 2021-01-06 13:31

This is similar to (but different from) this question.

Here is some simple test code to illustrate some weirdness I have discovered with Sun CC:

//--         


        
7条回答
  •  南笙
    南笙 (楼主)
    2021-01-06 14:08

    The const is void func1(const int) has no affect whatsoever on the function, since the int is passed by value --- a copy of the int is made, and that copy lives only as long as the function call. Whether or not you change that copy has no bearing on anything.

    You are probably confused by the fact that in void func2(const char*) the const is significant. But you have to recognized that is different from void func3(char* const). In the former, it's the character pointed to that cannot change; in the latter, it's the pointer that is (irrelevantly) 'const'

提交回复
热议问题