2.4 const qualifier & 2.5 dealing with types
来源:互联网 发布:gulp javascript 编辑:程序博客网 时间:2024/06/05 16:51
- Terminology: const Reference is a Reference to const
C++ programmers tend to abbreviate the phrase “reference to const” as“const reference.” This abbreviation makes sense—if you remember that itis an abbreviation.
Technically speaking, there are no const references. A reference is not anobject, so we cannot make a reference itself const. Indeed, because thereis no way to make a reference refer to a different object, in some sense allreferences are const. Whether a reference refers to a const or nonconsttype affects what we can do with that reference, not whether we can alterthe binding of the reference itself.
double dval = 3.14;int &r = dval; //illegalconst int &ri = dval; //legal
It is important to realize that a reference toconstrestricts only what we can dothrough that reference.
int i = 42;
int&r1=i;// r1 bound to iconst int &r2 = i;// r2 also bound to i; but cannot be used to change i
r1=0;// r1 is not const;i is now 0
r2 = 0;// error: r2 is a reference to const
low-level constis never ignored. When we copy an object,both objects must have the same low-levelconstqualification or there must be aconversion between the types of the two objects. In general, we can convert anonconsttoconstbut not the other way round.
const int *p = i;int *p1 = p; //illegal
To set “int *p = p3” illegal is to prevent inadvertently changing the value stored in p3 which is supposed to be const.const int *p = nullptr; // p is a pointer to a const int
constexpr int *q = nullptr; // q is a const pointer to int
constexpr const int *p = &i;
typedef double wages; // wages is a synonym for doubletypedef wages base, *p; // base is a synonym for double, p for double*using SI = Sales_item; // SI is a synonym for Sales_item
typedef char *pstring;
const pstring cstr = 0; // cstr is a constant pointer to char
const pstring *ps; //ps is a pointer to a constant pointer to charconst int ci = i, &cr = ci;auto b = ci; // b is an int (top-level const in ci is dropped)auto c = cr; // c is an int (cr is an alias for ci whose const is top-level) autod=&i; // d isan int*(& ofan int objectis int*)auto e = &ci; // e is const int*(& of a const object is low-level const)
If we want the deduced type to have a top-level const, we must say so explicitly:
const auto f = ci; // deduced type of ci is int; f has type const int
0 0
- 2.4 const qualifier & 2.5 dealing with types
- c++primer学习笔记(3)-Compound Types(pointers and references) and const Qualifier.
- Dealing with Adversity
- Dealing with WMI Timeouts (c#)
- XSLT(8) Dealing with namespace
- Dealing with Audio Output Hardware
- Pointers and the const Qualifier
- Pointers and the const Qualifier
- Pointers and the const Qualifier
- CurrencyManager Collection - Dealing with SuspendBinding and ResumeBinding
- Dealing with C++ "Unused Parameter" Warnings
- C# Tutorial - Dealing With Unhandled Exceptions
- 一些记录 when dealing with MP4 container
- Dealing with bitmap object in android NDK
- 英语练习140 Dealing with exams
- Files and Exceptions: Dealing with Errors
- 如何才能不崩溃 7: Dealing with Nothing
- Dealing with unbalanced data in machine learning
- CZY追女孩
- List of devices attached is followed by blank!
- MyBatis补充
- web.xml 详细介绍
- 向量
- 2.4 const qualifier & 2.5 dealing with types
- 图像去模糊(逆滤波)
- Windows平台下Makefile学习笔记
- Cloudera在Ubuntu的安装部署
- Hibernate HQL查询异常:java.lang.ClassCastException解决
- Android之——卸载应用程序
- [IOS] Object - Oriented
- LeetCode 题解(153): Surrounded Regions
- mac删除输入法