C++ primer 笔记
内容:
1. const
变量 buffSize是一个常量(512),如何给buffSize赋值的操作都是错误,并且由于无法对buffSize进行修改值,定义变量对时候必须初始化。
const int buffSize = 512;
auto &h = 42; // error
const auto &j = 42; // ok
1.1 Reference to const(const 的 引用)
引用就是变量的另外名字,const的引用可见下列代码
const int ci = 1024;
const int &r1 = ci;
r1 = 41; // error
const Reference is a Reference to const
const过程中的变量转换(double -> const int)
double dval = 3.14;
const int &ri = dval;
等价与
const int temp = dval;
const int &ri = temp;
所以,ri是temp变量的引用,不是dval的引用。
1.2 Pointer to const
a pointer to const may not be used to change the object to which the pointer points. We may store the address of a const object only in a pointer to const
const double pi = 3.14;
double *ptr = π // error, ptr is a plain pointer
const double &cptr = π // ok
Like a reference to const, a pointer to const says nothing about whether the object to which the pointer points is const. Defining a pointer as a pointer to const affects only what we can do with the pointer. It is important to remember that there is no guarantee that an object pointed to by a pointer to const won’t change.
Like any other const object, a const pointermust be initialized, and once initialized
int errNumb = 0;
int *const curErr = &errNumb;
const duoble pi = 3.14159;
// pip is a const pointer to a const object
const double *const pip = π
top-level const and low-level const
top-level : pointer itself is a constant
low-level : pointer can point to a const object
int i = 0;
int *const p1 = &i; // p1 is top-level
const int ci = 42; // ci is top-level
const int *p2 = &ci; // p2 is low-level
const int *const p3 = p2; // p3 is top-level
const int &r = ci; // const in reference types is always low -level
Exercise
int i = -1, &r = 0; // illegal, r must refer to an object.
int *const p2 = &i2; // legal.
const int i = -1, &r = 0; // legal.
const int *const p3 = &i2; // legal.
const int *p1 = &i2; // legal
const int &const r2; // illegal, r2 is a reference that cannot be const.
const int i2 = i, &r = i; // legal.
int i, *const cp; // illegal, cp must initialize.
int *p1, *const p2; // illegal, p2 must initialize.
const int ic, &r = ic; // illegal, ic must initialize.
const int *const p3; // illegal, p3 must initialize.
const int *p; // legal. a pointer to const int.
2. typedef
类型别名(Type Aliases)
简化与加深变量类型意义。
typedef double wages; // wage 就是 double
typedef wages base, *p; // base的类型是double,p是double *
“using” + “=“ 也是一种typedef
using SI = Sales_item;
SI item; // Same as Sales_item item
Pointer, const and Type Aliases
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 char
// wrong interpretation of const pstring cstr
const char *cstr = 0;
// cstr as a pointer to const char rather than as a const pointer to char
The auto type specifier
auto i = 0, *p = &i; // i is int and p is a pointer to int
auto sz = 0, pi = 3.14; // error i is int but pi is double
const int ci = i, &cr = ci;
auto b = ci; // b is int (top-level const is dropped)
auto c = cr; // c is int (top-level const is dropped)
auto d = &i; // d is int*
auto e = &ci // e is const int* (low-level)
3. data structures
自定义数据结构
若数据结构定义在头文件,则在开头需要引入头文件
#include <iostream>
#include <string>
// using the class of Sales_data
#include "Sales_data.h"
int main() {
Sales_data data1, data2;
return 0;
}
自定义数据结构也可以定义在主文件中。
Preprocessor ??