C++中const指针用法汇总
這里以int類型為例,進行說明,在C++中const是類型修飾符:
int a; 定義一個普通的int類型變量a,可對此變量的值進行修改。
const int a = 3;與 int const a = 3; 這兩條語句都是有效的code,并且是等價的,說明a是一個常量,不能對此常量的值進行修改。
const int* p =&a; 與 int const* p = &a; 這兩條語句都是有效的code,但是它們不是等價的。其中const int* p = &a; 是平時經常使用的格式,聲明一個指針,此指針指向的數據不能通過此指針被改變;int* const p = ∫ 聲明一個指針,此指針不能被改變以指向別的東西。
const int *a; 這里const修飾的是int,而int定義的是一個整值,因此*a所指向的對象值不能通過*a來修改,但是可以重新給a來賦值,使其指向不同的對象;
int *const a; 這里const修飾的是a,a代表的是一個指針地址,因此不能賦給a其他的地址值,但可以修改a指向的值;
int const *a;和const int *a;的意義是相同的,它們兩個的作用等價;
rules:
(1). A non-const pointer can be redirected to point to other addresses.
(2). A const pointer always points to the same address, and this address can not be changed.
(3). A pointer to a non-const value can change the value it is pointing to. These can not point to a const value.
(4). A pointer to a const value treats the value as const (even if it is not), and thus can not change the value it is pointing to.
以下是一些test code,詳細信息可見相關的reference:
#include <iostream>
#include "const_pointer.hpp"int test_const_pointer_1()
{
{ // reference: https://stackoverflow.com/questions/3247285/const-int-int-constint a = 5;int *p1 = &a; //non-const pointer, non-const dataconst int *p2 = &a; //non-const pointer, const data, value pointed to by p2 can’t changeint * const p3 = &a; //const pointer, non-const data, p3 cannot point to a different locationconst int * const p4 = &a; //const pointer, const data, both the pointer and the value pointed to cannot changeint const * const p5 = &a; // 與 const int * const p5等價
}{ // reference: https://stackoverflow.com/questions/162480/const-int-vs-int-const-as-function-parameter-in-c-and-c// read the declaration backwards (right-to-left):const int a1 = 1; // read as "a1 is an integer which is constant"int const a2 = 1; // read as "a2 is a constant integer"// a1 = 2; // Can't do because a1 is constant// a2 = 2; // Can't do because a2 is constantchar a = 'a';const char *s = &a; // read as "s is a pointer to a char that is constant"char const *y = &a; // 與 const char *y 等價char c;char *const t = &c; // read as "t is a constant pointer to a char"// *s = 'A'; // Can't do because the char is constants++; // Can do because the pointer isn't constant*t = 'A'; // Can do because the char isn't constant// t++; // Can't do because the pointer is constant// *y = 'A';y++;
}{ // reference: http://www.geeksforgeeks.org/const-qualifier-in-c/int i = 10;int j = 20;int *ptr = &i; /* pointer to integer */printf("*ptr: %d\n", *ptr);/* pointer is pointing to another variable */ptr = &j;printf("*ptr: %d\n", *ptr);/* we can change value stored by pointer */*ptr = 100;printf("*ptr: %d\n", *ptr);
}{ // const int *ptr <==> int const *ptrint i = 10;int j = 20;const int *ptr = &i; /* ptr is pointer to constant */printf("ptr: %d\n", *ptr);// *ptr = 100; /* error: object pointed cannot be modified using the pointer ptr */ptr = &j; /* valid */printf("ptr: %d\n", *ptr);
}{ // int * const ptrint i = 10;int j = 20;int *const ptr = &i; /* constant pointer to integer */printf("ptr: %d\n", *ptr);*ptr = 100; /* valid */printf("ptr: %d\n", *ptr);// ptr = &j; /* error */
}{ // const int *const ptr;int i = 10;int j = 20;const int *const ptr = &i; /* constant pointer to constant integer */printf("ptr: %d\n", *ptr);// ptr = &j; /* error */// *ptr = 100; /* error */
}{ // reference: http://www.learncpp.com/cpp-tutorial/610-pointers-and-const/int value = 5;int *ptr = &value;*ptr = 6; // change value to 6const int value2 = 5; // value is const// int *ptr2 = &value2; // compile error: cannot convert const int* to int*const int *ptr3 = &value2; // this is okay, ptr3 is pointing to a "const int"// *ptr3 = 6; // not allowed, we can't change a const valueconst int *ptr4 = &value; // ptr4 points to a "const int"fprintf(stderr, "*ptr4: %d\n", *ptr4);value = 7; // the value is non-const when accessed through a non-const identifierfprintf(stderr, "*ptr4: %d\n", *ptr4);
}return 0;
}//
// reference: https://en.wikipedia.org/wiki/Const_(computer_programming)
class C {int i;
public:int Get() const { // Note the "const" tagreturn i;}void Set(int j) { // Note the lack of "const"i = j;}
};static void Foo_1(C& nonConstC, const C& constC)
{int y = nonConstC.Get(); // Okint x = constC.Get(); // Ok: Get() is constnonConstC.Set(10); // Ok: nonConstC is modifiable// constC.Set(10); // Error! Set() is a non-const method and constC is a const-qualified object
}class MyArray {int data[100];
public:int & Get(int i) { return data[i]; }int const & Get(int i) const { return data[i]; }
};static void Foo_2(MyArray & array, MyArray const & constArray) {// Get a reference to an array element// and modify its referenced value.array.Get(5) = 42; // OK! (Calls: int & MyArray::Get(int))// constArray.Get(5) = 42; // Error! (Calls: int const & MyArray::Get(int) const)
}typedef struct S_ {int val;int *ptr;
} S;void Foo_3(const S & s)
{int i = 42;// s.val = i; // Error: s is const, so val is a const int// s.ptr = &i; // Error: s is const, so ptr is a const pointer to int// *s.ptr = i; // OK: the data pointed to by ptr is always mutable,// even though this is sometimes not desirable
}int test_const_pointer_2()
{C a, b;Foo_1(a, b);MyArray x, y;Foo_2(x, y);S s;Foo_3(s);return 0;
}
GitHub: https://github.com/fengbingchun/Messy_Test
總結
以上是生活随笔為你收集整理的C++中const指针用法汇总的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CUDA Samples:Vector
- 下一篇: 数学中的导数