C++ const question

后端 未结 7 1572
遥遥无期
遥遥无期 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:19

    See this question, this question, and this question.

    Basically, the const only means that the function will not modify the pointer's value. The pointers contents are not const, the same as the header's signature.

    0 讨论(0)
  • 2020-12-20 17:21

    In the first, you've promised the compiler, but not other users of the class that you will not edit the variable.

    In your second example, you've promised other users of the class that you will not edit their variable, but failed to uphold that promise.

    I should also note that there is a distinct difference between

    bar* const variable
    

    and

    const bar* variable
    

    and

    const bar* const variable
    

    In the first form, the pointer will never change, but you can edit the object that is pointed to. In the second form, you can edit the pointer(point it to another object), but never the variable that it points to. In the final form, you will neither edit the pointer, nor the object it points to. Reference

    To add a bit more of a clarification to the question stated, you can always promise MORE const than less. Given a class:

    class Foo {
        void func1 (int x);
        void func2 (int *x);
    }
    

    You can compile the following implementation:

    Foo::func1(const int x) {}
    Foo::func2(const int *x) {}
    

    or:

    Foo::func1(const int x) {}
    Foo::func2(const int* const x) {}
    

    without any problems. You've told your users that you may possibly edit their variables. In your implementation, you've told the compiler that this particular implementation will not edit those variables, even though the told the users you might. You haven't broken a promise to the user, and so the code compiles.

    0 讨论(0)
  • 2020-12-20 17:27

    The const keyword in the first example is meaningless. You are saying that you don't plan on changing the pointer. However, the pointer was passed by value and so it dos not matter if you change it or not; it will not effect the caller. Similarly, you could also do this:

    // In header 
    class Foo {
    void foo( int b );
    };
    
    // In cpp
    void Foo::foo( const int b ) {
    //Stuff
    }
    

    You can even do this:

    // In header 
    class Foo {
    void foo( const int b );
    };
    
    // In cpp
    void Foo::foo( int b ) {
    //Stuff
    }
    

    Since the int is passed by value, the constness does not matter.

    In the second example you are saying that your function takes a pointer to one type, but then implement it as taking a pointer to another type, therefore it fails.

    0 讨论(0)
  • 2020-12-20 17:32

    It probably doesn't care much about void Foo::foo(bar* const pBar) because how you treat the pointer itself (const or not) doesn't matter one bit outside of the routine. The C rules say that no change to pBar will travel outside of foo either way.

    However, if it is (const bar* pBar), that makes a difference, because it means the compiler is not to allow callers to pass in pointers to non-const objects.

    0 讨论(0)
  • 2020-12-20 17:33

    This is simpler to understand with a variable type other than a pointer. For example, you can have the following function declaration:

    void foo( int i );
    

    The definition can look like this:

    void foo( const int i ) { ... }
    

    Whether the variable 'i' is const or not on the definition side is an implementation detail. It has no impact for the clients of that function.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题