C++ const question

后端 未结 7 1585
遥遥无期
遥遥无期 2020-12-20 16:58

If I do this:

// In header 
class Foo {
void foo(bar*);
};

// In cpp
void Foo::foo(bar* const pBar) {
//Stuff
}

The compiler does not comp

7条回答
  •  甜味超标
    2020-12-20 17:33

    In the former, the const doesn't affect the interface, only the implementation. You are saying to the compiler, "I am not going to change the value of the bar* within this function". You can still change what is pointed to by the pointer. In the latter, you are telling the compiler (and all callers) that you will not change the bar structure that the bar* points to.

提交回复
热议问题