c++ primer


无符号不要与带符号的数字混合运算,这样会转化为无符号来运算

 cout << -1*1u << endl;//4294967295

常量的类型

  • 十进制的数字常量,按照数值大小 int->long->long long
  • 八进制和十六进制数字常量 int->unsigned int->long->unsigned long->long long->unsigned long long

初始化

int units_sold = 0;
int units_sold = {0};
int units_sold{0};//cx11
int units_sold(0);

long double ld = 3.1415926536;
int a{ld}, b = {ld}; // error: narrowing conversion required
int c(ld), d = ld; // ok: but value will be truncated

reference

A reference defines an alternative name for an object.

A reference type “refers to” another type.

A reference is not an object. Instead, a reference is just another name for an already existing object.Because references are not objects, we may not define a reference to a reference.

pointer

Because references are not objects, they don’t have addresses. Hence, we may not define a pointer to a reference.

The value (i.e., the address) stored in a pointer can be in one of four states:

  1. It can point to an object.
  2. It can point to the location just immediately past the end of an object.//arithmetic on pointer
  3. It can be a null pointer, indicating that it is not bound to any object.
  4. It can be invalid; values other than the preceding three are invalid.
int *p1 = nullptr; // equivalent to int *p1 = 0;   c0x11 only
int *p2 = 0; // directly initializes p2 from the literal constant 0
// must #include cstdlib
int *p3 = NULL; // equivalent to int *p3 = 0;

int i = 42;
int *p; // p is a pointer to int
int *&r = p; // r is a reference to the pointer p
r = &i; // r refers to a pointer; assigning &i to r makes p point to i
*r = 0; // dereferencing r yields i, the object to which p points; changes i to 0

The easiest way to understand the type of r is to read the definition right to left. The symbol closest to the name of the variable (in this case the & in &r) is the one that has the most immediate effect on the variable’s type. Thus, we know that r is a reference. The rest of the declarator determines the type to which r refers. The next symbol, * in this case, says that the type r refers to is a pointer type. Finally, the base type of the declaration says that r is a reference to a pointer to an int.

const

By Default, const Objects are Local to a File, if want cross file use extern on both its definition and declaration

const int ci = 1024;
const int &r1 = ci; // ok: both reference and underlying object are const
r1 = 42; // error: r1 is a reference to const
int &r2 = ci; // error: non const reference to a const object

Indeed, because there is no way to make a reference refer to a different object, in some sense all references are const.

int i = 42;
const int &r1 = i; // we can bind a const int& to a plain int object
const int &r2 = 42; // ok: r1 is a reference to const
const int &r3 = r1 * 2; // ok: r3 is a reference to const
int &r4 = r * 2; // error: r4 is a plain, non const reference

double dval = 3.14;
const int &ri = dval;//create a temporary const int from the double
int &ri = dval;//error

A Reference to const May Refer to an Object That Is Not const

int i = 42;
int &r1 = i; // r1 bound to i
const 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

Copyright © FengGuangtu 2017