C++ 中 const 使用

一 const解释

如果你一看见C++中const就脱口而出:“常量!”那只能说明你对c++不甚了解。或者说你太2了.
const得一些使用方法与场景如下:

1.const修饰普通变量,全局变量,静态变量

1 const int iTmp = 1;
2 int const iTmpEx = 2;

变量保持其原有属性,只是多了一个const,只读属性。这里,const到底是紧贴变量名还是类型说明符号(int)都无关紧要。在修饰指针时候的不同在后面会详细说明.

2.const修饰指针
const 修饰指针有三种情况:
只要看const离哪个近些,const离pName最近,说明它是在修饰指针变量pName,表示pName不能改变,即不能做pName++这种修改pTmp指针的操作。

//A: 指针指向内容不能修改,指针本身可以修改。
 char chBuffer[] = "test";
//等同char const* pName = chBuffer;
 const char *pName = chBuffer;
 pName++;      //合法,指针本身可以修改
 *pName = 'c';// 非法,指针指向的内容不能被修改
 cout<<pName<<endl();

//B: 指针指向内容能修改,指针本身不可以修改。
 char chBuffer[] = "test";
 char *  const pName = chBuffer;
 pName++;      //非法,指针本身不能被修改
 *pName = 'c'; //内容可以被修改
 cout<<pName<<endl();

//C: 指针指向内容不能修改,指针本身不可以修改。
 char chBuffer[] = "test";
 const char *  const pName = chBuffer;
 pName++;       //非法,指针本身不能被修改
 *pName = 'c'; //非法 内容不可以被修改
 cout<<pName<<endl();

3.const修饰函数参数

void Test(const int iAge)//函数体中,确保iAge不被修改。
void Test(const char *pName)//函数体中,确保pName指向的内容不被修改。
void Test(char* const  pName)//函数体中,确保pName不被修改。
void Test(const char* const  pName)//函数体中,确保pName和pName指向的内容不被修改。
void Test(const ClassTest &obj);// 这种情况使用非常多,这样可以避免调用ClassTest的构造函数,直接传递类对象的地址

c++中&和*怎么用
&引用访问一个变量是直接访问,而*指针是间接访问。
引用是一个变量的别名,本身不单独分配自己的内存空间,而指针有自己的内存空间。
引用在开始的时候就绑定到了一个内存空间(开始必须赋初值),所以他只能是这个内存空间的名字,而不能改成其他的,当然可以改变这个内存空间的值.
“&”是取地址运算符
“*”的用法有两种情况:(1)定义指针变量(2)间接访问运算符

int a,*p;
p=&a;
a=*p;

例如:

int i = 3,j = 4;
int &x = i;//成为i的别名
x = j;//不能否认x仍然引用i,并没有成为j的别名,只是修改了x和j共享的内存空间的值.这点与指针不同,指针在任何时刻都可以改变自己的指向

4.const修饰函数返回值
const 修饰返回值的用法比较少见,有种用法,比如

const char* Test();

那么接受它返回内容的变量也要写成const char * 类型
5:const修饰类成员变量
修饰类成员变量时候,初始化需要放到类初始化列表种进行初始化

class CTest{

public:
   int m_iAge;
}

CTest::CTest:m_iAge(99){
}

6.const修饰成员函数
对变量的只读访问,只是类普通变量,排除全局,静态变量。

class CTest{

public: 
   void Show()const;
private:
   int m_iAge;
}

void CTest:: Show()const{
 cout<<m_iAge;
// m_iAge++; 不能修改
}

原文:
1.C++ 中 const 使用

二 const与宏的区别

  1. 编译时刻
    宏是预编译(编译之前处理),const是编译阶段
  2. 编译检查.
    宏不做检查,不会报编译错误,只是替换,const会编译检查,会报编译错误。
  3. 优点.
    宏能定义一些函数,方法。 const不能。
  4. 缺点.
    用大量宏,容易造成编译时间久,每次都需要重新替换

三 static 作用

  1. 修饰局部变量
    • 延长局部变量的生命周期,程序结束才会销毁。
    • 某个局部变量(例如static int age = 0)只会生成一份内存,只会初始化一次,所有函数,共享这个局部变量(age)。
    • 改变局部变量的作用域,在其他函数中共享这个局部变量。static 表示静态的变量,分配内存的时候, 存储在静态区,不存储在栈上面。
  2. 修饰全局变量
    • 只能在本文件中访问,修改全局变量的作用域,生命周期不改变。
    • 避免重复定义全局变量

四 extern作用:

  1. 只是用来获取全局变量(包括全局静态变量)的值(例如:extern int externFile)),不能用于定义变量(例如:extern const int a = 20)。
  2. 先在当前文件查找有没有全局静态变量(static int a,因为文件中不能有int a形式的变量),没有找到,才会去其他文件查找全局变量(int a,其他文件里不包括静态变量,因为静态变量只能在本文件使用)。

五 static与const联合使用

  1. 声明一个静态的全局只读常量,其他文件无法访问,避免重复定义全局变量.

     static const int a = 20;
  2. 开发使用场景:在一个文件中经常使用的字符串常量,可以使用static与const组合.

    // GlobeConst.m中
    static const int a = 20;

六 extern与const联合使用

  1. 声明一个全局只读常量

     extern  const int  b = 1;
  2. 开发中使用场景:在多个文件中经常使用的同一个字符串常量,可以使用extern与const组合.只在头文件中做声明,不做定义。

     //GlobeConst.h中
     extern const int  b;
     //GlobeConst.m中
     const int  b = 1;

六 定义与声明的区别

声明不等于定义

//定义,指出变量名字同时为变量分配存储空间,定义包含了声明
int a = 20;
//声明,声明只是指出了变量的名字,并没有为其分配存储空间
 int a;

相关代码:

//
//  externFile.h
//  constStudy
//
//  Created by ifeng on 16/1/15.
//  Copyright © 2016年 beijing. All rights reserved.
//

#ifndef externFile_h
#define externFile_h

#include <stdio.h>

 extern int externFile;
 extern int externFileNoStatic;
#endif /* externFile_h */
//
//  externFile.c
//  constStudy
//
//  Created by ifeng on 16/1/15.
//  Copyright © 2016年 beijing. All rights reserved.
//

#include "externFile.h"
int externFile = 10;
int externFileNoStatic = 12;
// 全局变量:只有一份内存,所有文件共享,与extern联合使用。
int a = 20;
// static修饰全局变量
static int age = 20;
static int externFile = 11;
//引用外部变量
extern int  externFile;//引用的是本文件的externFile
extern int externFileNoStatic;//引用的是其他文件的externFile
//int externFileNoStatic;不能这样声明,错误。

void test()
{
    // static修饰局部变量
    static int age = 0;
    age++;
    int a = 5;
    a ++;
    printf("static:%d,int a:%d\n",age,a);
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        printf("Hello, World!\n");
        test();//static:1,int a:6
        test();//static:2,int a:6
        printf("static2:%d,int a :%d\n",age,a);//static2:20,int a :20
        extern int age;
        printf("extern in file:%d\n",age);//extern in file:20
       printf("externFile in file:%d\n",externFile);//externFile in file:11
        printf("externFileNoStatic out file:%d\n",externFileNoStatic);//externFile out file:12

    }
    return 0;
}

1.C/C++中extern关键字详解
2全局变量和extern详解
3.【如何正确使用const,static,extern】|那些人追的干货

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值