C++:const用法

 首先,欢迎并感谢博友进行知识补充与修正。

#include <iostream>

using namespace std;


//#define在编译预处理阶段处理
//const常量是由编译器处理的,提供类型检查和作用域检查
#define b 20
int main01()
{
	/*int a=10;
	int b=20;
	int array[a + b];*/  //linux内核里是成立的,原因是编译linux内核的gcc编译器支持
						//c和c++均不支持这种现象
	const int a=10;
	//const int b=20;
	int array[a + b];

	system("pause");
	return 0;
}

//const常量是由编译器处理的,提供类型检查和作用域检查
void fun1()
{
	#define c 10
	const int d = 20;
	//# undef c  卸载宏
}

void fun2()
{
	cout << "c=" << c << endl;	//可以调用
	//cout << "d=" << d << endl;	//无法调用
}
int main()
{
	fun2();
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/feissss/article/details/88122647