本文主要介绍C++中的 const 关键字相关知识。
1. 概述
const 关键字的作用是把一个对象定义为一个常量。
2. 用法
2.1 将变量定义为常量
在 C++ 中,有两种简单的定义常量的方式:
- 使用 #define 预处理器。
- 使用 const 关键字。
使用 const 关键字声明指定类型的变量为常量,一般用法如下:
const type variable = value;
上述语句将type类型的变量variable声明为常量,常量值为value。
本文主要介绍C++中的 const 关键字相关知识。
const 关键字的作用是把一个对象定义为一个常量。
在 C++ 中,有两种简单的定义常量的方式:
使用 const 关键字声明指定类型的变量为常量,一般用法如下:
const type variable = value;
上述语句将type类型的变量variable声明为常量,常量值为value。