C++11 细节知识点
Initializers
4 ways to initialize variables:
1 | int units_sold = 0; |
The us of {} was introdcuded by C++11。 It is called list initialization.
When used with variables of built-in type, this form of initialization has one important property: The compiler will not let us list initialize variables of built-in type if the initializer might lead to the loss of information:
1 | long double ld = 3.1415926536; |
Default Initializaiton
Variables defined outside any function body are initialized to zero. With one exception, which we cover in § 6.1.1 (p. 205), variables of built-in type defined inside a function are uninitialized.
Most classes let us define objects without explicit initializers. Such classes supply an appropriate default value for us. For example, as we’ve just seen, the library string class says that if we do not supply an initializer, then the resulting string is the empty string:
1 | std::string empty; // empty implicitly initialized to the empty string |
Uninitialized objects of built-in type defined inside a function body have undefined value. Objects of class type that we do not explicitly initialize have a value that is defined by the class.
Variable Declarations and Definitiions
We recommend initializing every object of built-in type. It is not always necessary, but it is easier and safer to provide an initializer until you can be certain it is safe to omit the initializer.
To support separate compilation, C++ distinguishes between declarations anddefinitions. A declaration makes a name known to the program. A file that wants to use a name defined elsewhere includes a declaration for that name. A definition creates the associated entity.
A variable declaration specifies the type and name of a variable. A variable definition is a declaration. In addition to specifying the name and type, a definition also allocates storage and may provide the variable with an initial value. To obtain a declaration that is not also a definition, we add the extern keyword and may not provide an explicit initializer:
1 | extern int i; // declares but does not define i |
Any declaration that includes an explicit initializer is a definition. We can provide an initializer on a variable defined as extern, but doing so overrides the extern. An extern that has an initializer is a definition:
1 | extern double pi = 3.1416; // definitio |
It is an error to provide an initializer on an extern inside a function. Variables must be defined exactly once but can be declared many times.
Key Concept: Static Typing
C++ is a statically typed language, which means that types are checked atcompile time. The process by which types are checked is referred to as type checking.
Identifiers
Identifiers(标识符) in C++ can be composed of letters, digits, and the underscore character. The language imposes no limit on name length. Identifiers must begin with either a letter or an underscore. Identifiers are case-sensitive; upper- and lowercase letters are distinct:
1 | // defines four different int variables |
Scope of a Name
A scope is a part of the program in which a name has a particular meaning.
const Qualifier
By Default, const Objects Are Local to a File
To define a single instance of a const variable, we use the keyword extern on both its definition and declaration(s):
1 | // file_1.cc defines and initializes a const that is accessible to other files |
In this program, file_1.cc defines and initializes bufSize. Because this declaration includes an initializer, it is (as usual) a definition. However, because bufSize is const, we must specify extern in order for bufSize to be used in other files.
To share a const object among multiple files, you must define the variable as extern.
References to const
As with any other object, we can bind a reference to an object of a const type. To do so we use a reference to const, which is a reference that refers to a const type. Unlike an ordinary reference, a reference to const cannot be used to change the object to which the reference is bound:
1 | const int ci = 1024; |
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 it is an abbreviation.
Technically speaking, there are no const references. A reference is not an object, so we cannot make a reference itself const. Indeed, because there is no way to make a reference refer to a different object, in some sense all references are const. Whether a reference refers to a const or nonconst type affects what we can do with that reference, not whether we can alter the binding of the reference itself.”
A Reference to const May Refer to an Object That Is Not const
It is important to realize that a reference to const restricts only what we can do through that reference. Binding a reference to const to an object says nothing about whether the underlying object itself is const. Because the underlying object might be nonconst, it might be changed by other means:
1 | int i = 42; |
Pointers and const
Pointers can point to either const or nonconst types. A pointer to const (§ 2.4.1, p. 61) 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:
1 | const double pi = 3.14; // pi is const; its value may not be changed |
const Pointers
Unlike references, pointers are objects. Hence, as with any other object type, we can have a pointer that is itself const. Like any other const object, a const pointer must be initialized, and once initialized, its value (i.e., the address that it holds) may not be changed. We indicate that the pointer is const by putting the const after the *. This placement indicates that it is the pointer, not the pointed-to type, that is const:
1 | int errNumb = 0; |
The fact that a pointer is itself const says nothing about whether we can use the pointer to change the underlying object. Whether we can change that object depends entirely on the type to which the pointer points. For example, pip is a const pointer to const. Neither the value of the object addressed by pip nor the address stored in pip can be changed. On the other hand, curErr addresses a plain, nonconst int. We can use curErr to change the value of errNumb:
1 | *pip = 2.72; // error: pip is a pointer to const |
Pointers, const, and Type Aliases
Declarations that use type aliases that represent compound types and const can yield surprising results. For example, the following declarations use the type pstring, which is an alias for the the type char*:
1 | typedef char *pstring; |
The base type in these declarations is const pstring. As usual, a const that appears in the base type modifies the given type. The type of pstring is “pointer to char.” So, const pstring is a constant pointer to char—not a pointer to const char. It can be tempting, albeit incorrect, to interpret a declaration that uses a type alias by conceptually replacing the alias with its corresponding type:
1 | const char *cstr = 0; // wrong interpretation of const pstring cstr |
However, this interpretation is wrong. When we use pstring in a declaration, the base type of the declaration is a pointer type. When we rewrite the declaration using char*, the base type is char and the ** is part of the declarator. In this case,const char* is the base type. This rewrite declares cstr as a pointer to const char *rather than as a const pointer to char.