const

1、使用const来定义常量

const int num = 10; //应该在声明时进行初始化,否则该常量的值是不确定的,而且无法修改

2、const与指针

指向常量的指针(const修饰的是指针指向的内容)

//指向常量的指针
double rates[5] = {88.9, 100.12, 59.2, 111.1, 23.2};
const double *pd = rates;
cout << pd[2] << "\n";
pd[2] = 67.99; //error
cout << pd[2];

pd为指向常量的指针,

不能使用pd来修改它指向的数值,但可以让pd指向其他的地址。通常把指向常量的指针用作函数参量。

指针常量(const 修饰指针)

//指针常量
double rates[5] = {88.9, 100.12, 59.2, 111.1, 23.2};
cout << pd[2] << "\n";
double * const pd2 = rates;
pd2[2] = 67.99; //ok
cout << pd[2];

pd2的值不可改变,只能指向最初赋给它的地址,但pd2可以用来改变它所指向的数据值。

<<effective C++>>上有个好记的方法:const在*号左边修饰的是指针所指的内容;const在*号右边修饰的是指针。
简单记就是:左内容,右指针。
也可以这样理解:先忽略类型名,然后看const离哪个近,就修饰谁。
如:const [int] *p; //先去掉int,这时const 修饰*p, p是指针,*p是指针指向的对象,不可变。

3、const与引用

double value = 100;

const double & v = value;

不能修改const引用所指向的内容

4、const与类成员

class StaticAndConstTest
{
public:
StaticAndConstTest(int _num = 100);
~StaticAndConstTest(void);
//类的const 成员常量 必须在构造方法中使用初始化列表来初始化
const int num;
}; StaticAndConstTest::StaticAndConstTest(int _num)
: num(_num)
{
}

 const成员函数

class StaticAndConstTest
{
public:
StaticAndConstTest(int _num = 100);
~StaticAndConstTest(void);
//const成员函数
void show() const;
//类的const 成员 必须在构造方法中使用初始化列表来初始化
const int num;
}; void StaticAndConstTest::show() const
{
std::cout << num;
}

const成员函数不能修改调用对象,只要类方法不修改调用对象,就应将其声明为const。

static

1、static局部变量

void test()
{
//static 局部变量 在程序的整个生命周期存在,只有一份,作用域为函数局部
static int num = 1000;
num++;
cout << "num=" << num << "\n";
}
int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 0; i < 3; i++)
{
test();
} return 0;
}

输出结果:

aaarticlea/png;base64,iVBORw0KGgoAA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YYddPB5w94HAgEAoGcFoKcEuQ0wTzq5wKBQCCQx4gQpwQ5BQoGAoFAICdMiFNCUDAQCAQCOXGgYCAQCATyQICCgUAgEMgDIcTJUDAQCAQCOXlCnBLKQMFAIBAI5KRBMgry0AUzuHPlfvbHPIQnDIFAIJBjgzz0GcygFUZt2DzKeegYCAQCOaUgnIJwSvahCGZU2TF0l/4B50d9LgQCgUBOESivoLwmmIP7UaOOD21hHfBofWf67j/q/NDPhUAgEMjpYlAw6vljHVuGdbRGcbBgwLBW2NDHH6UcCAQCgZwKUF5BHoBgLAN0vuIowQz1ytDzg597Xy8BBAKBQB4Ep6eCGfqwo770AY8GgUAgkFMBKiiooGQrj14wgx8e5TwUDAQCgZxSwkL7UMGA3mLiUAGMwjLAqJuOdf7evnEIBAKBPFgwoR0W2rkK3MkPgUAgkBMFE9phHgoGAoFAICcNJrQxvp2rtKFgIBAIBHKSYEIbE6BgIBAIBHLSYNk2loWCgUAgEMgJ8/8BhtJYcxEp/nIAAAAASUVORK5CYII=" alt="" />

静态局部变量属于静态存储方式,在函数内定义它的生存期为整个程序生命周期,但是其作用域仍与自动变量相同,只能在定义该变量的函数内使用该变量。
若在声明时未赋以初值,则系统自动赋予0值。而对自动变量不赋初值,则其值是不定的。

2、static全局变量

//静态全局变量 只能在定义它的文件中使用,外部无法访问
static int g_NUM = 100;
非静态全局变量的作用域是整个源程序,也即在各个源文件中都是有效的。

3、static与类

static数据成员与static成员函数函数

class StaticAndConstTest
{
public:
StaticAndConstTest(int _num = 100);
~StaticAndConstTest(void);
//const成员函数
void show() const;
//静态成员函数 属于类,
//无法访问属于类对象的no-static数据成员,也无法访问no-static成员函数,它只能调用其余的静态成员函数。
static double GetNum();
//类的const 成员 必须在构造方法中使用初始化列表来初始化
const int num;
//类的静态成员变量,属于类,所有类对象共享
static double NUM_2;
//静态成员常量
static const double NUM_3;
//静态成员是整型const可以在类声明中初始化,其他类型不可以
static const int NUM_4 = 4; }; //静态全局变量 只能在定义它的文件中使用,外部无法访问
static int g_NUM = 100;
//静态成员变量在类声明中声明,只能在类定义方法的文件中初始化
double StaticAndConstTest::NUM_2 = 100.1;
//静态成员常量
const double StaticAndConstTest::NUM_3 = 200.1;

C++学习笔记-const和static的更多相关文章

  1. ES6-11学习笔记--const

    新声明方式:const 1.不属于顶层对象 window 2.不允许重复声明 3.不存在变量提升 4.暂时性死区 5.块级作用域   以上特性跟let声明一样,特性可看 let 的学习笔记:链接跳转 ...

  2. C语言学习笔记--const 和 volatile关键字

    1.const关键字 (1)const 修饰的变量是只读的,它不是真正的常量,本质还是变量,只是告诉编译器不能出现在赋值号左边! (2)const 修饰的局部变量在栈上分配空间 (3)const 修饰 ...

  3. js-ES6学习笔记-const命令

    1.const声明一个只读的常量.一旦声明,常量的值就不能改变. 2.const声明的变量不得改变值,这意味着,const一旦声明变量,就必须立即初始化,不能留到以后赋值. 3.const的作用域与l ...

  4. c语言学习笔记 const变量

    在c语言的编程过程中经常会遇到有常数参加运算的运算,比如这种. int a=100*b; 这个100我们叫常数或者叫常量,但是程序中我们不推荐这种直接写常数的方法,有两个缺点. 第一是程序可读性差. ...

  5. C++学习笔记-const

    const在C++中有着大量的运用,深刻理解const有助于进一步理解C++. const基础知识 int main() { const int a;//C++中必须初始化 int const b;/ ...

  6. 《OOC》笔记(1)——C语言const、static和extern的用法

    <OOC>笔记(1)——C语言const.static和extern的用法 C语言中const关键字用法不少,我只喜欢两种用法.一是用于修饰函数形参,二是用于修饰全局变量和局部变量. 用c ...

  7. 0013 Java学习笔记-面向对象-static、静态变量、静态方法、静态块、单例类

    static可以修饰哪些成员 成员变量---可以修饰 构造方法---不可以 方法---可以修饰 初始化块---可以修饰 内部类(包括接口.枚举)---可以修饰 总的来说:静态成员不能访问非静态成员 静 ...

  8. ES6学习笔记<一> let const class extends super

    学习参考地址1  学习参考地址2 ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准.因为当前版本的ES6是在2015年发布的,所以又称ECMAScript 2015:也 ...

  9. JavaScript:学习笔记(7)——VAR、LET、CONST三种变量声明的区别

    JavaScript:学习笔记(7)——VAR.LET.CONST三种变量声明的区别 ES2015(ES6)带来了许多闪亮的新功能,自2017年以来,许多JavaScript开发人员已经熟悉并开始使用 ...

随机推荐

  1. php -- PHP5中file_get_contents函数获取带BOM的utf-8文件内容

    最近,在用file_get_contents函数来取得文本的内容的时候,出现了一个情况(如下),苦思冥想了n久,不得其解,最后,果然还是得靠百度啊..... 百度到一个解释,下面是原文: PHP5中的 ...

  2. GitHub Permission to <<repository>> denied to <<username>>

    I kept receiving a 403 error saying my usual username couldn’t access the repository with my usual a ...

  3. linux下面bin,sbin不理解的查阅

    在一下的文件中得到答案, 突然想想自己有点傻,自己有代码,为什么不自己查看一下代码呢 http://blog.csdn.net/ithomer/article/details/9839957

  4. hdu 1425:sort(排序,经典题。快排模板)

    sort Time Limit : 6000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submissi ...

  5. mysql更改utf8编码方式

    方法1: 一.查看数据库编码格式 1 mysql> show variables like 'character_set_database'; 二.查看数据表的编码格式 1 mysql> ...

  6. SQL TRIM()函数去除字符串头尾空格

    SQL TRIM()函数去除字符串头尾空格 SQL 中的 TRIM 函数是用来移除掉一个字串中的字头或字尾.最常见的用途是移除字首或字尾的空白.这个函数在不同的资料库中有不同的名称: MySQL: T ...

  7. 四 Android Studio打包EgretApp (热更新)

    官网教程: http://developer.egret.com/cn/github/egret-docs/Native/native/hotUpdate/index.html 和Eclipse一样, ...

  8. MYSQL常用命令(转载)

    1.导出整个数据库 mysqldump -u 用户名 -p --default-character-set=latin1 数据库名 > 导出的文件名(数据库默认编码是latin1)mysqldu ...

  9. jquery remove() detach() empty()三种方法的区别

    remove方法把事件删除掉了,数据并没有删除 detach方法保存了事件和数据 empty方法保留了元素本身,移除子节点,删除内容 举例: <!DOCTYPE html><html ...

  10. CH5105 Cookies【贪心】【线性dp】

    5105 Cookies 0x50「动态规划」例题 描述 圣诞老人共有M个饼干,准备全部分给N个孩子.每个孩子有一个贪婪度,第 i 个孩子的贪婪度为 g[i].如果有 a[i] 个孩子拿到的饼干数比第 ...