如何在 C++ 中定义常量?
你可以在变量声明之前添加 const 限定符来在 C++ 中定义常量。
实例
#include<iostream>
using namespace std;
int main() {
const int x = 9;
x = 0;
return 0;
}这将定义常量变量 x。但它会抛出一个错误,因为我们正在尝试重写常量的值。
广告
你可以在变量声明之前添加 const 限定符来在 C++ 中定义常量。
#include<iostream>
using namespace std;
int main() {
const int x = 9;
x = 0;
return 0;
}这将定义常量变量 x。但它会抛出一个错误,因为我们正在尝试重写常量的值。