首页 > 编程语言 >C++ const pointer

C++ const pointer

时间:2023-01-28 21:33:06浏览次数:43  
标签:const 变量 指向 int C++ 限定 pointer 指针

在C++中const限定的指针类型常常令人困惑,现整理如下,以整型为例,主要区分如下三个例子

const int * p;
int * const p;
const int * const p;

其实就是2种情况,const在int前及const在变量名前,第三种及叠加了前两种限定

 

CONST概念

const即constant缩写,用于表示所限定的变量不能改变

 

CONST与指针

在前面的例子中,如果将p指向一个变量val,则无论是哪一个例子,变量val本身并不受指向它的指针的类型限定影响,可以任意改变

const int *

    int b = 2;
    int bb = 22;
    const int * const_p = &b;
//    (*const_p)++; // error
    const_p = &bb;

在这种限定下,表示所限定的指针对指向的变量只有只读权限(变量自身可以改变),但可以改变指针自身,即指向另一个变量

int * const

    int c = 3;
    int cc = 33;
    int * const p_const = &c;
    (*p_const)++;
//    p_const = &cc; // error

在这种限定下,表示所限定的指针自身不能改变,即不能指向别的变量,但是可以通过指针去修改指向的变量

const int * const

叠加了上面两个限制

演示代码

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int main(){
 6 // non const
 7     int a = 1;
 8     int aa = 11;
 9     int * p = &a;
10 
11     cout << "a = " << a << "; p point to a, *p = " << *p << endl;
12 
13     a++;
14     cout << "\tafter a++" << endl;
15     cout << "a = " << a << "; p point to a, *p =" << *p << endl;
16 
17     (*p)++;
18     cout << "\tafter (*p)++" << endl;
19     cout << "a = " << a << "; p point to a, *p= " << *p << endl;
20 
21     p = &aa;
22     cout << "\tafter p = &aa" << endl;
23     cout << "a = " << a << ", aa = " << aa << "; p point to aa, *p = " << *p << endl;
24 
25 
26 // const int *
27     int b = 2;
28     int bb = 22;
29     const int * const_p = &b;
30 
31     cout << endl << "now switch to 'const int *'" << endl;
32     cout << "b = " << b << "; const_p point to b, *const_p = " << *const_p << endl;
33 
34     b++;
35     cout << "\tafter b++" << endl;
36     cout << "b = " << b << "; const_p point to b, *const_p = " << *const_p << endl;
37 
38 //    (*const_p)++; // error: increment of read-only location '* const_p'
39     cout << "\t(*const_p)++: error: increment of read-only location '* const_p'" << endl;
40 
41     const_p = &bb;
42     cout << "\tafter const_p = &bb" << endl;
43     cout << "b = " << b << ", bb = " << bb << "; const_p point to bb, *const_p = " << *const_p << endl;
44 
45 
46 // int * const
47     int c = 3;
48     int cc = 33;
49     int * const p_const = &c;
50 
51     cout << endl << "now switch to 'int * const'" << endl;
52     cout << "c = " << c << "; p_const point to c, *p_const = " << *p_const << endl;
53 
54     c++;
55     cout << "\tafter c++" << endl;
56     cout << "c = " << c << "; p_const point to c, *p_const = " << *p_const << endl;
57 
58     (*p_const)++;
59     cout << "\tafter (*p_const)++"  << endl;
60     cout << "c = " << c << "; p_cosnt point to c, *p_const = " << *p_const << endl;
61 
62 //    p_const = &cc; // error: assignment of read-only variable 'p_const'
63     cout << "\tp_const = &cc: error: assignment of read-only variable 'p_const'" << endl;
64 
65 
66 // const int * cosnt
67     int d = 4;
68     int dd = 44;
69     const int * const const_p_const = &d;
70 
71     cout << endl << "now switch to 'const int * const'" << endl;
72     cout << "d = " << d << "; const_d_const point to d, *const_p_const = " << *const_p_const << endl;
73 
74     d++;
75     cout << "\tafter d++" << endl;
76     cout << "d = " << d << "; const_d_const point to d, *const_p_const = " << *const_p_const << endl;
77 
78 //    (*const_p_const)++; // error: increment of read-only location '*(const int*)const_p_const'
79     cout << "\t(*const_p_const)++: error: increment of read-only location '*(const int*)const_p_const'" << endl;
80 
81 //    const_p_const = &cc; // error: assignment of read-only variable 'const_p_const'
82     cout << "\tconst_p_const = &dd: error: assignment of read-only variable 'const_p_const'" << endl;
83 
84     return 0;
85 }

 

标签:const,变量,指向,int,C++,限定,pointer,指针
From: https://www.cnblogs.com/qingkai/p/17071113.html

相关文章

  • 关于 Dev-C++ 中缺少 iconv.h 的问题
    前言在C++中有个扩展库ext,里面有一些黑科技(hash,splay,binomial_heap等等),在Windows环境中,我们运行Dev-C++并在头文件写#include<bits/extc++.h>时,经常会收到......
  • C++函数文档注释模板
    还是.net好,///就解决了点击查看代码///<summary>///在指定的node结点之后插入新结点,如果node为NULL,表示新结点插在链表第一个结点之前///</summary>///<paramna......
  • 大海捞针 Skia(C++):Skia 环境搭建
    前言笔者曾经编译过一款使用了Skia的软件,于是查询了一些资料,了解到Skia是一个2D向量图形处理函数库。只是可惜,笔者尝试用它写程序,但是官方文档国内无法访问,网上资料极少,并......
  • OpenMP 线程同步 Construct 实现原理以及源码分析(上)
    OpenMP线程同步Construct实现原理以及源码分析(上)前言在本篇文章当中主要给大家介绍在OpenMP当中使用的一些同步的construct的实现原理,如master,single,critica......
  • 蓝桥杯 易错题 特殊时间 c++
    问题描述2022年2月22日22:20是一个很有意义的时间,年份为2022,由3个2和1个0组成,如果将月和日写成4位,为0222,也是由3个2和1个0组成,如果将时间中的......
  • C++Day13 tinyxml2解析rss文件
    一、任务与思路使用tinyxml解析rss文件,使用std::regex(正则表达式)去除html标签,并生成一个pagelib.txt,格式如下<doc><docid>1</docid><title>...</title><......
  • 用VC++访问XML文件
    用微软的DOM,MSXML4//引入msxml4.dll#import"C:/WINNT.0/system32/msxml4.dll"//创建XMLDOMDocument指针MSXML2::IXMLDOMDocumentPtrpXMLDoc;//初始化COM接口::C......
  • Xmake v2.7.6 发布,新增 Verilog 和 C++ Modules 分发支持
    Xmake是一个基于Lua的轻量级跨平台构建工具。它非常的轻量,没有任何依赖,因为它内置了Lua运行时。它使用xmake.lua维护项目构建,相比makefile/CMakeLists.txt,配置语......
  • C/C++工业数据分析与文件信息管理系统
    C/C++工业数据分析与文件信息管理系统大连理工大学《程序设计基础A课程设计》设计报告工业数据分析与文件信息管理系统学生姓名:院系、班级:学号:联系电......
  • C++算术计算器[2023-01-27]
    C++算术计算器[2023-01-27]面向对象程序设计C++作业考核一、考核内容使用C++语言,设计开发一个算术计算器,能够根据用户输入计算输出表达式结果。二、基本要求1.能够支......