What's the point of const pointers?

后端 未结 17 1828
粉色の甜心
粉色の甜心 2020-11-30 17:28

I\'m not talking about pointers to const values, but const pointers themselves.

I\'m learning C and C++ beyond the very basic stuff and just until today I realized t

相关标签:
17条回答
  • 2020-11-30 17:47

    I guess an advantage would be that the compiler can perform more aggressive optimizations inside the function knowing that this pointer cannot change.

    It also avoids eg. passing this pointer to a subfunction which accepts a non-const pointer reference (and could therefore change the pointer like void f(int *&p)), but I agree, that the usefulness is somewhat limited in this case.

    0 讨论(0)
  • 2020-11-30 17:50

    The top level const qualifier is discarded in declarations, so the declarations in the question declare exactly the same function. On the other hand, in the definition (implementation) the compiler will verify that if you mark the pointer as const, it is not modified inside the body of the function.

    0 讨论(0)
  • 2020-11-30 17:52

    I make a point of using only const arguments because this enables more compiler checks: if I accidentally re-assign an argument value inside the function, the compiler bites me.

    I rarely reuse variables, it’s cleaner to create new variables to hold new values, so essentially all my variable declarations are const (except for some cases such as loop variables where const would prevent the code from working).

    Note that this makes only sense in the definition of a function. It doesn’t belong in the declaration, which is what the user sees. And the user doesn’t care whether I use const for parameters inside the function.

    Example:

    // foo.h
    int frob(int x);
    
    // foo.cpp
    int frob(int const x) {
       MyConfigType const config = get_the_config();
       return x * config.scaling;
    }
    

    Notice how both the argument and the local variable are const. Neither is necessary but with functions that are even slightly larger, this has repeatedly saved me from making mistakes.

    0 讨论(0)
  • 2020-11-30 17:55

    Your question touches on something more general: Should function arguments be const?

    The constness of value arguments (like your pointer) is an implementation detail, and it does not form part of the function declaration. This means that your function is always this:

    void foo(T);
    

    It is entirely up to the implementer of the function whether she wants to use the functions-scope argument variable in a mutable or in a constant way:

    // implementation 1
    void foo(T const x)
    {
      // I won't touch x
      T y = x;
      // ...
    }
    
    // implementation 2
    void foo(T x)
    {
      // l33t coding skillz
      while (*x-- = zap()) { /* ... */ }
    }
    

    So, follow the simple rule to never put const in the declaration (header), and put it in the definition (implementation) if you don't want or need to modify the variable.

    0 讨论(0)
  • 2020-11-30 17:55

    Types of declaring any variables like-
    (1)Declaring a constant variable.
    DataType const varibleName;

     int const x;
        x=4; //you can assign its value only One time
    (2)Declare a pointer to a constant variable
    const dataType* PointerVaribleName=&X;
     const int* ptr = &X;
         //Here pointer variable refer contents of 'X' that is const Such that its cannot be modified
    dataType* const PointerVaribleName=&X;
     int* const ptr = &X;
         //Here pointer variable itself is constant  Such that value of 'X'  can be modified But pointer can't be modified

    0 讨论(0)
  • 2020-11-30 17:56

    Passing a const pointer to a function makes little sense, as it will be passed by value anyways. It's just one of those things that are allowed by the general language design. Prohibiting it just because it doesn't make sense would just make the language spec. larger.

    If you are inside a function it is of course another case. Having a pointer that cannot change what it points to is an assertion that makes the code clearer.

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