广州网站建设 推广公司,四大门户网站现状,微信小程序开发需要什么,免费ppt模板资源C const关键字有多种用法
可以用来修饰变量、指针、函数参数、成员函数等。可以看到const在C中有多种用法#xff0c;主要用于保证数据的不可变性#xff0c;增强代码的安全性和可读性。在实际编程中#xff0c;根据需要选择适当的const用法#xff0c;可以有效避免意外修…C const关键字有多种用法
可以用来修饰变量、指针、函数参数、成员函数等。可以看到const在C中有多种用法主要用于保证数据的不可变性增强代码的安全性和可读性。在实际编程中根据需要选择适当的const用法可以有效避免意外修改数据提高程序的稳定性
以下是一些常见的用法及其示例
1. 修饰普通变量
const可以用来声明常量即变量的值在初始化后不能被修改。
#include iostreamint main() {const int num 10;// num 20; // Error: cannot assign to variable num with const-qualified type const intstd::cout num: num std::endl;return 0;
}2. 修饰指针
const修饰指针时有多种情况取决于const的位置。
2.1 指向常量的指针
指针本身可以改变但不能通过该指针修改它所指向的值。
#include iostreamint main() {int value 10;const int *ptr value;// *ptr 20; // Error: read-only variable is not assignablevalue 20; // Allowed, since value itself is not constptr nullptr; // Allowed, ptr itself is not conststd::cout value: value std::endl;return 0;
}2.2 常量指针
指针本身是常量但可以修改它所指向的值。
#include iostreamint main() {int value 10;int *const ptr value;*ptr 20; // Allowed, can modify the value pointed to// ptr nullptr; // Error: cannot assign to variable ptr with const-qualified type int *conststd::cout value: value std::endl;return 0;
}
2.3 指向常量的常量指针
指针本身和指向的值都不能改变。
#include iostreamint main() {int value 10;const int *const ptr value;// *ptr 20; // Error: read-only variable is not assignable// ptr nullptr; // Error: cannot assign to variable ptr with const-qualified type const int *conststd::cout value: value std::endl;return 0;
}3. 修饰函数参数
const可以用来修饰函数参数以保证函数内部不能修改参数的值。
3.1 按值传递的常量参数
这种情况虽然参数在函数内部不能修改但因为按值传递函数外部的变量不受影响。
#include iostreamvoid printValue(const int value) {// value 20; // Error: cannot assign to variable value with const-qualified type const intstd::cout Value: value std::endl;
}int main() {int num 10;printValue(num);return 0;
}
3.2 按引用传递的常量参数
这种情况既可以避免不必要的复制又保证了函数内部不能修改参数的值。
#include iostreamvoid printValue(const int value) {// value 20; // Error: cannot assign to variable value with const-qualified type const int std::cout Value: value std::endl;
}int main() {int num 10;printValue(num);return 0;
}
4. 修饰成员函数
const成员函数表示该函数不会修改对象的状态不能修改类的成员变量除非是用mutable关键字修饰的变量。
#include iostreamclass MyClass {
public:int getValue() const {// value 20; // Error: cannot assign to non-static data member within const member functionreturn value;}void setValue(int v) {value v;}private:int value 10;
};int main() {MyClass obj;std::cout Value: obj.getValue() std::endl;obj.setValue(20);std::cout Value: obj.getValue() std::endl;return 0;
}
5. 顶层const和底层const
顶层constTop-level const指对象本身是常量例如 const int a 10;。 底层constLow-level const指对象的内容是常量例如 const int *ptr;。
6. 修饰返回类型
const可以修饰返回类型防止返回值被修改。
#include iostreamclass MyClass {
public:const int getValue() const {return value;}private:int value 10;
};int main() {MyClass obj;const int val obj.getValue();// val 20; // Error: cannot assign to variable val with const-qualified type const int std::cout Value: val std::endl;return 0;
}
7. 修饰常量表达式
constexpr是C11引入的用于声明常量表达式保证表达式在编译时计算。
#include iostreamconstexpr int square(int x) {return x * x;
}int main() {const int result square(5);std::cout Result: result std::endl;return 0;
}