一const与指针
1指针常量
int*constptr;
指针常量,即不可以将ptr修改为指向内存中另一处的地址,但可以用它来改变它所指向的地址处的数据内容。
//const.cpp
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int temp = 4;
cout << temp << endl;
//ptr被const修饰 定义时必须初始化
int *const ptr = &temp;
*ptr = 5;
cout << temp << endl;
return 0;
}
程序运行截图:
2常量指针
constint*ptr;(常量指针的另一种写法intconst*ptr)
常量指针,即可以将ptr修改为指向内存中另一处的地址,但不可以用它来改变它所指向的地址处的数据内容。
//const.cpp
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int temp1 = 4;
int temp2 = 5;
const int *ptr;
ptr = &temp1;
cout << "*ptr:" << *ptr << endl;
ptr = &temp2;
cout << "*ptr:" << *ptr << endl;
return 0;
}
程序运行截图:
3指向const常量的const指针
constint*constptr;(另一种写法intconst*constptr)
指向const常量的const指针,即ptr所指向的内存地址和所指向的地址处的数据内容都不能被修改
PS:const离谁比较近,则谁的值不能被改变
二const与函数
1const修饰函数的参数
const使该参数在函数中不能被改变
voidcopy(char*strDes,constchar*strsrc);
即strsrc指针所指向的值在copy函数中不能被改变,如果意外的改动该指针,编译器会报错
注意:
不用将“值传递”的方式(voidcopy(intx,iny))加const修饰,因为函数会自动产生临时变量用于复制该参数,参数本来就不需要保护。
2const修饰函数的返回值
constint*add(intx,inty);
即add函数的返回值(即指针)的内容不能被修改,该返回值只能被赋值给加const修饰的同类型指针
constint*p=add(3,5);
注意:
如果函数返回值是“值传递方式”(intadd(intx,inty)),由于函数会把返回值复制到外部临时的存储单元中,加const修饰没有意义。
3const成员函数
任何不会修改类的数据成员(即函数中的变量)的函数都应该声明为const类型。在编写const成员函数时,如果不小心修改了数据成员,或者调用了其他非const成员函数,编译器会报错,这种做法将大大提高程序的健壮性。
//test.cpp
class test
{
public:
void set(int);
//const成员函数
int get()const;
private:
int value;
};
int test::get()const
{
//编译错误 修改数据成员value
++value;
//编译错误 调用了非const函数
set(5);
return value;
}
分享到:
相关推荐
编译原理PL0源码(C语言版) /*** PL0 COMPILER WITH CODE GENERATION ***/ //--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //----...
c/c++语言中const的用法,const 定义变量,指针的用法和不同
zlib使用DEFLATE算法,最初是为libpng函式库所写的,后来普遍为许多软件所使用。此函式库为自由软件,使用zlib授权。截至2007年3月,zlib是包含在Coverity的美国国土安全部赞助者选择继续审查的开源项目。 qlibzip...
使用方法如下: const Promise = require("../es6-promise.js"); 即可创建 ,new Promise()
运算符重载,可作为模板使用 完整的程序,易读的代码。 BigInteger operator +(const BigInteger& B); BigInteger operator -(const BigInteger& B); BigInteger operator *(const BigInteger& B); BigInteger ...
C/C++面试之算法系列--几个典型的内存拷贝及字符串函数实现 写一个函数,完成内存之间的拷贝。[考虑问题是否全面,是否考虑内存重叠问题] 返回void *支持链式操作,参数类型是void *以支持任意类型的指针,输入...
static const unsigned int step = 16; private: static char viewChar (const char ch); static std::string viewHead (int i); static std::string viewHexes (const std::string &str;); static std::...
C++ learning ---const用法总结,将平时不同情况的const用法,进行了详细的总结。是学习C++的好帮手
主要介绍了C/C++中CONST用法总结(推荐),包括const常量与define宏定义的区别介绍,非常不错,具有参考借鉴价值,需要的朋友参考下吧
C/C++图实现运动会比赛安排 int map[49];//邻接矩阵,使用一维数组表示,下标=行*结点数+列 int color[7];//存入每个项目的颜色号 const char *n[6] = { "张凯","王刚","李四","张三","王峰","李杰" };//参赛人员...
2011年2月7日 - 顶点着色器例子——Diffuse Lighting(《龙书》) #include "d3dUtility.h" // // Globals // IDirect3DDevice9* Device = 0; const int Width = 640;
用法 import PixivAppApi from 'pixiv-app-api' //const PixivAppApi = require("pixiv-app-api") import pixivImg from 'pixiv-img' //const pixivImg = require("pixiv-img") const pixiv = new PixivAppApi ( ...
const romchar AT_AT[] = {"AT\r\n"}; const romchar AT_CBC[] ={"AT+CBC\r\n"}; //检查充电状态,以及电池电量占容量的百分比+CBC: 0,97,4164 const romchar AT_CREG[] = {"AT+CREG?\r\n"}; //查询网络注册状态 ...
c++ 的array源码分析和reverse-iterator和-Array-const-iterator类
精心整理,大家在使用VC++处理字符串的时候肯定遇到unicode 宽字符的问题: 比如:“不能将参数 1 从“const char [5]...总之就是LP表示指针,C表示const,STR表示char,WSTR表示wchar_t,TSTR表示TCHAR...............
自己秋招根据面经和基础知识学习总结的知识点,主要内容包括但不限于:volatile关键字、sizeof & strlen、...对于C/C++/C++11/C++14的内容都有涉及,但肯定没法做到100%知识点全覆盖,可以根据自己需要后续添加内容。
t vcpuid,void * vcpuctx,const char * data,uint32_t length,const void ** out,uint32_t * out_length){//插入消息前缀。 // std :: string msg =“ [vmx] vcpu-” + std :: to_string(vcpuid)+“:”; ...
使用 将Redux Store绑定到App上。 const store = createStore(reducer) // redux store const WeAppRedux = require('./libs/wechat-weapp-redux/index.js'); const {Provider} = WeAppRedux; Prov
如何使用 产生 import anonymous from 'anonymous-animals-gen' const { name , image , animal } = anonymous . generate ( ) ; // name -> elastic giraffe // image -> [removed]... // animal -> giraffe const...
最初的目标-------------*熟悉C++(再次)。*支持GET和POST方法。*允许OAuth的1.0认证。建立在Windows* x86和x64。*支持PUT和DELETE方法。* Yammer的API和其他可能的例子。长期---------*序列化的支持。*支持C + + ...