在 C++ 中 , const 可以作为 替代 #define 宏定义 的手段 ;
const int a = 10;
#define a 10
const 关键字 与 #define 宏定义 相同点 :
在 C 语言中 , 定义数组 , 如果数组的大小不是常数 , 在 C 语言 和 C++ 语言 中都会在编译时报错 ;
错误代码示例 :
// 包含 C++ 头文件
#include "iostream"
// 使用 std 标准命名空间
// 该命名空间中 , 定义了很多标准定义
using namespace std;
// 导入 C 头文件
#include <stdio.h>
int main()
{
int a = 10;
int b = 20;
int array[a + b];
// 控制台暂停 , 按任意键继续向后执行
//system("pause");
return 0;
}
执行后报错信息如下 :
已启动生成…
1>------ 已启动生成: 项目: HelloWorld, 配置: Debug Win32 ------
1>hello_world.cpp
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(15,14): error C2131: 表达式的计算结果不是常数
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(15,12): message : 因读取超过生命周期的变量而失败
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\hello_world.cpp(15,12): message : 请参见“a”的用法
1>已完成生成项目“HelloWorld.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
使用 const 常量作为 数组的大小 , 编译会通过 ;
代码示例 :
// 包含 C++ 头文件
#include "iostream"
// 使用 std 标准命名空间
// 该命名空间中 , 定义了很多标准定义
using namespace std;
// 导入 C 头文件
#include <stdio.h>
int main()
{
const int a = 10;
const int b = 20;
int array[a + b];
// 控制台暂停 , 按任意键继续向后执行
//system("pause");
return 0;
}
执行结果 :
使用 宏定义 作为 数组的大小 , 编译会通过 ;
代码示例 :
// 包含 C++ 头文件
#include "iostream"
// 使用 std 标准命名空间
// 该命名空间中 , 定义了很多标准定义
using namespace std;
// 导入 C 头文件
#include <stdio.h>
#define a 10
#define b 20
int main()
{
int array[a + b];
// 控制台暂停 , 按任意键继续向后执行
//system("pause");
return 0;
}
执行结果 :
在 C++ 语言中 , const 关键字 与 #define 宏定义 不同点 :
在下面的代码中 , 只要调用了 fun1 函数 , 执行了 #define a 10
代码 , 那么在后续不管哪个函数中 , 都可以调用 a 宏定义值 ;
但是在 fun1 函数中 定义了 常量 b , 代码为 const int b = 20;
, 只能在 fun1 函数中调用该常量 , 在其它函数中是无法调用该常量 b 的 ;
代码示例 :
// 包含 C++ 头文件
#include "iostream"
// 使用 std 标准命名空间
// 该命名空间中 , 定义了很多标准定义
using namespace std;
// 导入 C 头文件
#include <stdio.h>
void fun1()
{
#define a 10
const int b = 20;
}
void fun2()
{
printf("a = %d\n", a);
//printf("b = %d\n", b);
}
int main()
{
fun1();
fun2();
// 控制台暂停 , 按任意键继续向后执行
//system("pause");
return 0;
}
执行结果 :
a = 10
D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\Debug\HelloWorld.exe (进程 24920)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .
标准的做法是 , 在函数中使用了 宏定义 a , 如果在函数结尾不再使用该 宏定义 , 那么可以卸载该宏定义 , 使用 #undef a 可卸载宏定义 , 使用 #undef 可卸载所有宏定义 ;
void fun1()
{
#define a 10 // 定义宏定义
const int b = 20;
#undef a // 卸载宏定义
}