const与#define

来源:互联网 发布:记账软件电脑版 编辑:程序博客网 时间:2024/06/14 21:30

const与#define

#include "stdafx.h"#include <iostream>using namespace std;#define N 200 //宏,在预处理时发生了替换 不接受类型检查//const 永远不会发生改变//const 意义上相当于 #define#if 01.C++中的const 是真const 是为了取代#define 定义的宏常量2.const 修饰的变量称为常变量,常(不可变 用任何方法均不可变)变量(类型的概念 可以接受类型的检查)3.const 修饰指针4.const 修饰引用const == >const &none const == > const &const type & == > !type(常量、表达式等)5.脱const化const_cast<typeid>(待转化类型)typeid 的类型必须为引用或指针,不可以直接应用与对象//对const 类型的脱const后的指针和引用的 写操作c++未定义//如i++ + i++  a[i++]=i++;const int a = 5;为了实现 int &ra = a;第一种方式:const int &ra = a;第二种方式:int &ra = const_cast<int&>(a);#endif int _tmain(int argc, _TCHAR* argv[]){const int a = 200; //编译阶段发生了替换 也就是在以后过程中//凡是用到a的地方 就相当于把a直接换成了200int b = 10;int c;c = b + a;//c = b + 200;cout << c << endl;return 0;}


原创粉丝点击