C++:Const正确性和指针参数

Ben*_*Ben 21 c++ pointers const function

我知道const指针可以通过几种方式声明:

const int * intPtr1; // Declares a pointer that cannot be changed.
int * const intPtr2; // Declares a pointer whose contents cannot be changed.

// EDIT: THE ABOVE CLAIMS ARE INCORRECT, PLEASE READ THE ANSWERS.
Run Code Online (Sandbox Code Playgroud)

但是在函数参数的上下文中,相同的原理呢?

我认为以下是多余的:

void someFunc1(const int * arg);
void someFunc2(int * arg);
Run Code Online (Sandbox Code Playgroud)

由于someFunc 1和2对指针本身执行pass-by-value,因此someFunc1在给定函数调用中不可能更改原始指针的值.为了显示:

int i = 5;
int * iPtr = &i;

someFunc1(iPtr); // The value of iPtr is copied in and thus cannot be changed by someFunc1.
Run Code Online (Sandbox Code Playgroud)

如果这些都是真的,那么用'const int*ptr'类型arg声明一个函数是没有意义的,对吗?

Mik*_*our 34

你倒退了:

const int * intPtr1; // Declares a pointer whose contents cannot be changed.
int * const intPtr2; // Declares a pointer that cannot be changed.
Run Code Online (Sandbox Code Playgroud)

以下const确实是不必要的,并且没有理由将它放在函数声明中:

void someFunc1(int * const arg);
Run Code Online (Sandbox Code Playgroud)

但是,您可能希望将其放在函数实现中,原因与您可能想要声明局部变量(或其他任何东西)相同const- 当您知道某些事情不会发生变化时,实现可能更容易理解.无论是否const在函数的任何其他声明中声明它,都可以这样做.

  • @ yes123:完全没有区别.`const`限定它之前的东西,除非它在开头,在这种情况下它首先符合条件.所以在这两种情况下,它都限定了`int`,而不是指针.`int*const`将限定指针. (7认同)

hav*_*exz 25

好吧,它不适用于调用者,而是适用于内部的代码someFunc1.所以里面的任何代码都someFunc1不会意外地改变它.喜欢

void someFunc1(int *arg) {
  int i = 9;
  arg = &i;  // here is the issue
  int j = *arg;
}
Run Code Online (Sandbox Code Playgroud)

让我们做一些案例研究:

1)只需使指向值const

void someFunc1(const int * arg) {
int i = 9;
*arg = i; // <- compiler error as pointed value is const
}
Run Code Online (Sandbox Code Playgroud)

2)只需使指针const

void someFunc1(int * const arg) {
int i = 9;
arg = &i; // <- compiler error as pointer is const
}
Run Code Online (Sandbox Code Playgroud)

3)如果涉及的变量可以是const,则使用const的正确方法:

void someFunc1(const int * const arg) {
    int i = 9;
    *arg = i; // <- compiler error as pointed value is const
    arg = &i; // <- compiler error as pointer is const
}
Run Code Online (Sandbox Code Playgroud)

这应该清除所有的疑虑.所以我已经提到它是用于功能代码而不是用于调用者,你应该使用我上面提到的3种情况中最严格的.

编辑:

  • 即使在函数声明中,它也是一种很好的惯例const.这不仅会提高可读性,而且调用者也会意识到合同,并且对参数的不变性更有信心.(这是必需的bcoz你通常共享你的头文件,所以调用者可能没有你的实现c/cpp文件)
  • 如果声明和定义同步,甚至编译器也可以指出更好.


mat*_*way 7

你的逻辑是错误的.你应该倒着读的类型,所以const int *是一个指向const intint * const是一个const指针为int.

例:

void foo() {
    int a = 0;
    int b = 0;

    int * const ptrA = &a;
    *ptrA = 1;
    ptrA = &b; ///< Error

    const int * ptrB = &a;
    *ptrB = 1; ///< Error
    ptrB = &b;

    const int * const ptrC = &a;
    *ptrC = 1; ///< Error
    ptrC = &a; ///< Error
}
Run Code Online (Sandbox Code Playgroud)

要详细说明为什么你希望你的函数参数是a,const int *你可能想要告诉调用者他们必须传入一个int因为你作为一个函数想要更改值.例如,考虑以下代码:

void someFunc1(const int * arg) {
    // Can't change *arg in here
}

void someFunc2(int * arg) {
    *arg = 5;
}

void foo() {
    int a = 0;
    someFunc1(&a);
    someFunc2(&a);

    const int b = 0;
    someFunc1(&b);
    someFunc2(&b); ///< *** Error here. Must pass in an int not a const int.
}
Run Code Online (Sandbox Code Playgroud)