C++ Primer 读书笔记1(二)

2015-01-26 23:12:53 · 作者: · 浏览: 23
that the function uses or might use.

13、the type of a reference and theobject to which the reference refers must match exactly. Moreover, for reasonswe’ll explore in § 2.4.1, a reference may be bound only to an object, not to aliteral or to the result of a more general expression:

int &refVal4 = 10;   // error: initializer must be an object 
double dval = 3.14; 
int &refVal5 = dval; // error: initializer must be an int object
14、A pointer is a compound type that “points to” another type. Like references, pointers are used forindirect access to other objects. Unlike a reference, a pointer is an object inits own right. Pointers can be assigned and copied; a single pointer can pointto several different objects over its lifetime. Unlike a reference, a pointerneed not be initialized at the time it is defined. Like other built-in types,pointers defined at block scope have undefined value if they are notinitialized.

15、There are several ways toobtain a null pointer:

int *p1 = nullptr; // equivalent to int *p1 = 0;
int *p2 = 0;       // directly initializes p2 from the literal constant 0 
// must #include cstdlib
int *p3 = NULL;    // equivalent to int *p3 = 0;
ModernC++ programs generally should avoid using NULL and use nullptr instead.

16、A reference is not an object.Hence, we may not have a pointer to a reference. However, because a pointer isan object, we can define a reference to a pointer:

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 wayto understand the type of r is to read the definition right to left. The symbolclosest to the name of the variable (in this case the & in &r) is theone that has the most immediate effect on the variable’s type. Thus, we knowthat r is a reference. The rest of the declarator determines the type to whichr refers. The next symbol, * in this case, says that the type r refers to is apointer type. Finally, the base type of the declaration says that r is areference to a pointer to an int.

17、Because we can’t change theva lue of a const object after we create it, it must be initialized. As usual,the initializer may be an arbitrarily complicated expression:
const int i = get_size();  // ok: initialized at run time 
const int j = 42;          // ok: initialized at compile time 
const int k;               // error: k is uninitialized const

Among theoperations that don’t change the value of an object is initialization― when weuse an object to initialize another object, it doesn’t matter whether either orboth of the objects are consts:

int i = 42; 
const int ci = i;    // ok: the value in i is copied into ci 
int j = ci;          // ok: the value in ci is copied into j
18
// file_1.cc defines and initializes a const that is accessible to other files 
extern const int bufSize = fcn(); 
// file_1.h 
extern const int bufSize; // same bufSize as defined in file_1.cc

Note:To share aconst object among multiple files, you must define the variable as extern.

19、 References to const

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

we noted thatthere are two exceptions to the rule that the type of a reference must matchthe type of the object to which it refers. The first exception is that we caninitialize a reference to const from any expression that can be converted (§2.1.2, p. 35) to the type of the reference. In particular, we can bind are