活动介绍
file-type

C++中const关键字详解与应用

下载需积分: 9 | 10KB | 更新于2024-12-30 | 94 浏览量 | 9 评论 | 1 下载量 举报 收藏
download 立即下载
在C++编程中,`const`关键字是至关重要的,特别是对于初学者来说,理解其正确使用方法有助于编写更清晰、可维护的代码。`const`用于声明常量,它有以下几个关键点: 1. **常量修饰符**: - `const`可以用来修饰变量,表示该变量的值在编译时就已经确定,不能在程序运行过程中改变。例如,`const float pi = 3.1415926;`定义了一个常数浮点数,其值固定为π。 2. **限制修改**: - 使用`const`修饰的变量,不能被重新赋值。尝试像`float const pi = 3.1415926;`或`const float const pi = 3.1415926;`这样的语法错误,是因为编译器不允许对常量进行赋值操作。 3. **常量指针**: - 常量指针(如`const char* str = "foo";`)指向的是不可更改的字符数据,但指针本身是可以改变的。这意味着你可以改变指针所指向的对象,但不能改变指针本身。如果试图将一个`const`类型的指针赋值给非`const`类型的指针,如`char *const str = "foo";`,则需要特殊处理。 4. **const成员函数**: - 在类中,`const`前缀可以用于成员函数,表示它们不会修改对象的状态。例如,`const char* const foo(const char* const str) const;`定义了一个只读的成员函数,它接收一个常量字符串参数并返回一个常量指针,保证了函数内部不改变外部传入的数据。 5. **const与可变对象**: - 当使用`const`与可变对象交互时,需要特别注意。如`strcpy`函数接受一个`const char*`类型的源,所以在调用时需要确保目标地址是可写入的,如`strcpy((char*)str, "ABC");`。如果直接对`const`对象执行写操作,会引发编译错误。 6. **const的深浅拷贝**: - 对于`const`对象,只有浅拷贝是安全的,因为深拷贝可能会试图修改不可变的对象。例如,`const char* str = "foo";`拷贝时只会复制指向的字符串地址,不会复制字符串内容。 7. **const的灵活性**: - 虽然`const`表示常量,但C++允许在某些情况下动态地改变`const`属性。例如,`char* strTmp = str; strcpy(strTmp, "ABC");`,这里通过赋值操作实现了对`str`的临时修改。不过,这通常不适用于`const`对象的深层结构。 8. **const的作用与限制**: - `const`主要用来提高代码的可读性和安全性,减少意外修改。但并不能完全阻止底层数据的改变,只是在编译时就明确表明了意图。例如,虽然`char * const`阻止了指针本身的变化,但不能阻止指针指向的数据被修改。 9. **const与constexpr**: - C++11引入了`constexpr`,它在编译期间就能计算结果的表达式,而不仅仅是运行时。这对于模板、初始化列表等场景特别有用,但不是`const`的直接扩展。 了解并正确使用`const`关键字在C++编程中至关重要,它能帮助程序员编写更加清晰、高效和安全的代码。记住,`const`不仅是关于变量值的锁定,更是关于代码行为的约束,遵循良好的编程习惯,可以使代码更具可维护性。

相关推荐

filetype

编译g2o,输入make后../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::parent_path() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::directory_iterator::operator*() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::has_parent_path() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::_M_find_extension() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::symlink_status(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::has_root_directory() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::status(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::_M_split_cmpts()' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::directory_iterator::operator++()' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::directory_iterator::directory_iterator(std::filesystem::__cxx11::path const&, std::filesystem::directory_options, std::error_code*)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::read_symlink(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::replace_extension(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::has_filename() const' collect2: error: ld returned 1 exit status g2o/examples/data_fitting/CMakeFiles/curve_fit.dir/build.make:99: recipe for target 'bin/curve_fit' failed make[2]: *** [bin/curve_fit] Error 1 CMakeFiles/Makefile2:1924: recipe for target 'g2o/examples/data_fitting/CMakeFiles/curve_fit.dir/all' failed make[1]: *** [g2o/examples/data_fitting/CMakeFiles/curve_fit.dir/all] Error 2 make[1]: *** Waiting for unfinished jobs....../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::parent_path() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::directory_iterator::operator*() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::has_parent_path() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::_M_find_extension() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::symlink_status(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::has_root_directory() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::status(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::_M_split_cmpts()' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::directory_iterator::operator++()' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::directory_iterator::directory_iterator(std::filesystem::__cxx11::path const&, std::filesystem::directory_options, std::error_code*)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::read_symlink(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::replace_extension(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::has_filename() const' collect2: error: ld returned 1 exit status g2o/examples/data_fitting/CMakeFiles/circle_fit.dir/build.make:99: recipe for target 'bin/circle_fit' failed make[2]: *** [bin/circle_fit] Error 1 CMakeFiles/Makefile2:1896: recipe for target 'g2o/examples/data_fitting/CMakeFiles/circle_fit.dir/all' failed make[1]: *** [g2o/examples/data_fitting/CMakeFiles/circle_fit.dir/all] Error 2Makefile:155: recipe for target 'all' failed make: *** [all] Error 2

filetype

../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::parent_path() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::directory_iterator::operator*() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::has_parent_path() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::_M_find_extension() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::symlink_status(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::has_root_directory() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::status(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::_M_split_cmpts()' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::directory_iterator::operator++()' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::directory_iterator::directory_iterator(std::filesystem::__cxx11::path const&, std::filesystem::directory_options, std::error_code*)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::read_symlink(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::replace_extension(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::has_filename() const' collect2: error: ld returned 1 exit status g2o/apps/g2o_cli/CMakeFiles/g2o_cli_application.dir/build.make:99: recipe for target 'bin/g2o' failed make[2]: *** [bin/g2o] Error 1 CMakeFiles/Makefile2:1230: recipe for target 'g2o/apps/g2o_cli/CMakeFiles/g2o_cli_application.dir/all' failed make[1]: *** [g2o/apps/g2o_cli/CMakeFiles/g2o_cli_application.dir/all] Error 2 Makefile:155: recipe for target 'all' failed make: *** [all] Error 2

filetype

编译安装g2o时,cmake .. make之后../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::parent_path() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::directory_iterator::operator*() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::has_parent_path() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::_M_find_extension() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::symlink_status(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::has_root_directory() const' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::status(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::_M_split_cmpts()' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::directory_iterator::operator++()' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::directory_iterator::directory_iterator(std::filesystem::__cxx11::path const&, std::filesystem::directory_options, std::error_code*)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::read_symlink(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::replace_extension(std::filesystem::__cxx11::path const&)' ../../../lib/libg2o_stuff.so.0.2.0: undefined reference to `std::filesystem::__cxx11::path::has_filename() const' collect2: error: ld returned 1 exit status g2o/apps/g2o_cli/CMakeFiles/g2o_cli_application.dir/build.make:99: recipe for target 'bin/g2o' failed make[2]: *** [bin/g2o] Error 1 CMakeFiles/Makefile2:1230: recipe for target 'g2o/apps/g2o_cli/CMakeFiles/g2o_cli_application.dir/all' failed make[1]: *** [g2o/apps/g2o_cli/CMakeFiles/g2o_cli_application.dir/all] Error 2 Makefile:155: recipe for target 'all' failed make: *** [all] Error 2

资源评论
用户头像
woo静
2025.05.07
初学者必读,const关键字用法一目了然。👋
用户头像
13572025090
2025.04.20
文档清晰展示了const修饰符的用法和规则。
用户头像
赵小杏儿
2025.03.17
对C++ const有疑惑?这个文档或许能给你答案。
用户头像
郑瑜伊
2025.02.25
C++编程者提升基础的优质学习资源。
用户头像
学习呀三木
2025.02.23
适合初学者,是掌握C++ const用法的好资料。
用户头像
武藏美-伊雯
2025.02.17
文件虽小,但含金量高,是入门const的好选择。
用户头像
南小鹏
2025.02.16
通过实例深入理解const在C++中的重要性。
用户头像
又可乐
2025.02.09
文档详细讲解了C++中const的多种应用场景。👋
用户头像
FloritaScarlett
2024.12.28
经典讲解,const在编程中的注意事项。