C/C++:CV限定的理解

本文详细介绍了C/C++中的const与volatile关键字。const关键字主要用于声明符号常量,并能修饰常量的作用域和链接性;volatile关键字则用于改善编译器的优化能力,确保变量值的实时更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章来源于:http://jingyan.baidu.com/article/f0062228dcc8a6fbd3f0c88a.html


C/C++提供多种声明变量和函数存储持续性、作用域和链接性的关键字,有些被称为存储说明符(store class specifier)或 cv 限定符(cv-qualifier),这里就一起学习一下cv限定符

cv限定符就是constvolatile,其中最常用的是const。下面分别介绍它们:

const目前共有两种用途:

1、声明符号常量

2、修饰常量的作用域链接性。(C++)

volatile的作用就是改善编译器的优化能力。

一、const用途1:声明符号常量

  1. 使用const创建符号常量的通用格式如下:

    const type name = value;

    如:

    const int months = 12;

    const bool TRUE = 1;

  2. 需要注意的是在声明符号常量的同时必须进行初始化,否则就没有机会再给符号常量赋值了。

    如果在声明符号常量时没有初始化,那么该常量的值将是不确定的,且无法修改!

  3. 复杂一些的const用法如:

    const char* const week[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

    上面声明语句中第一个const是为了防止字符串被修改,第二个const则防止指针数组中的指针指向的内容被修改。

  4. C++与C中的const也是有区别的,不同之处在于:

    C++中const可以修饰常量的作用域

    C++中const声明的常量可以用作数组长度。

  5. 很多人习惯用#define语句完成符号常量的声明和定义,但是使用const更有优势:

    1、使用const声明符号常量能够指明类型

    2、C++中使用const声明符号常量会限定该常量的作用域

    3、const可以用于数组结构

    因此,以前使用#define来定义符号常量的话,请不要继续这样做,应使用const

二、const用途2:修饰常量的作用域和链接性

  1. 在C++中,在文件中声明的全局变量默认链接性是外部的,但使用const定义的全局变量链接性是内部的,如:

    const int time = 12;

    则常量time只在声明它的文件或代码段内可见,就像在定义时添加了static一样:

    static const int time = 12;

  2. 2

    假如一个常量需要在默认作用域的外部使用,则可以使用extern关键字来修改默认的内部链接性

    extern const int time = 12;

    这样在一个文件中声明的常量time就可以在另外一个文件中使用,需要注意的是其他文件中使用time时必须使用extern

    extern int time;

    因为time在多个文件中共享,所以只能有一个文件对其进行初始化


三、volatile关键字

  1. volatile不像const那么常用,因此很多人对它不是很了解。下面就介绍一下volatile的作用:

  2. 关键字volatile表明,即使程序代码没有对内存单元进行修改,其值也可能发生变化。

    听上去令人费解,其实该关键字的作用就是改善编译器的优化能力。

  3. 详细说明:

    假如你的程序中在多处使用同一个变量,那么编译器编译的时候可能进行优化,即将这个变量的值缓存到寄存器中(为了加快读取速度),而不是让程序查找这个值多次,编译器做出这种优化的前提是认为这个变量在多次使用的过程中不会发生改变。

    可以想象,如果未加防范,编译器做出了这种优化,而程序在某处改变了这个变量的值,则其后欲使用该变量初始值的地方使用了储存在寄存器中改变后的值,后果可想而知。

    想要避免这种情况发生就可以使用volatile关键字声明变量:

    volatile int time = 24;

    相当于告诉编译器,不要对time进行这种优化。


a.cpp:9:49: error: non-member function ‘Complex operator-(Complex&)’ cannot have cv-qualifier 9 | friend Complex operator-( Complex &c3 ) const;//重载双目运算符'-' | ^~~~~ a.cpp:25:9: error: no declaration matches ‘Complex Complex::operator-=(Complex&) const’ 25 | Complex Complex::operator-=(Complex &c2)const | ^~~~~~~ a.cpp:8:17: note: candidate is: ‘Complex Complex::operator-=(Complex&)’ 8 | Complex operator-=( Complex &c2 ); //重载双目运算符'-=' | ^~~~~~~~ a.cpp:3:7: note: ‘class Complex’ defined here 3 | class Complex | ^~~~~~~ a.cpp:31:9: error: no declaration matches ‘Complex Complex::operator-(Complex&) const’ 31 | Complex Complex::operator-(Complex &c3)const | ^~~~~~~ a.cpp:31:9: note: no functions named ‘Complex Complex::operator-(Complex&) const’ a.cpp:3:7: note: ‘class Complex’ defined here 3 | class Complex | ^~~~~~~ a.cpp: In function ‘int main()’: a.cpp:44:12: error: no match for ‘operator-’ (operand types are ‘Complex’ and ‘Complex’) 44 | c3 = c1-c2; | ~~^~~ | | | | | Complex | Complex In file included from /usr/include/c++/11/bits/stl_algobase.h:67, from /usr/include/c++/11/bits/char_traits.h:39, from /usr/include/c++/11/ios:40, from /usr/include/c++/11/ostream:38, from /usr/include/c++/11/iostream:39, from a.cpp:1: /usr/include/c++/11/bits/stl_iterator.h:577:5: note: candidate: ‘template<class _IteratorL, class _IteratorR> constexpr decltype ((__y.base() - __x.base())) std::operator-(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)’ 577 | operator-(const reverse_iterator<_IteratorL>& __x, | ^~~~~~~~ /usr/include/c++/11/bits/stl_iterator.h:577:5: note: template argument deduction/substitution failed: a.cpp:44:13: note: ‘Complex’ is not derived from ‘const std::reverse_iterator<_IteratorL>’ 44 | c3 = c1-c2; | ^~ In file included from /usr/include/c++/11/bits/stl_algobase.h:67, from /usr/include/c++/11/bits/char_traits.h:39, from /usr/include/c++/11/ios:40, from /usr/include/c++/11/ostream:38, from /usr/include/c++/11/iostream:39, from a.cpp:1: /usr/include/c++/11/bits/stl_iterator.h:1693:5: note: candidate: ‘template<class _IteratorL, class _IteratorR> constexpr decltype ((__x.base() - __y.base())) std::operator-(const std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)’ 1693 | operator-(const move_iterator<_IteratorL>& __x, | ^~~~~~~~ /usr/include/c++/11/bits/stl_iterator.h:1693:5: note: template argument deduction/substitution failed: a.cpp:44:13: note: ‘Complex’ is not derived from ‘const std::move_iterator<_IteratorL>’ 44 | c3 = c1-c2; | ^~ a.cpp:9:24: note: candidate: ‘Complex operator-(Complex&)’ 9 | friend Complex operator-( Complex &c3 ) const;//重载双目运算符'-' | ^~~~~~~~ a.cpp:9:24: note: candidate expects 1 argument, 2 provided 确
最新发布
05-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值