首页 > 代码库 > c++ primer learning point.1
c++ primer learning point.1
1.when we wanna share const variables; we use the keyword extern
on both its definition and declaration(s):
2.Once initialized, a reference remains bound to its initial object. There is no way to rebind a reference to refer to a different object. Because there is no way to rebind a reference, references must be initialized.
3.Reference is not an object. it only share the address of lvalue.
4.the most important is the understanding of inner ‘reference & pointer‘ mechanism.Its about the address.
keywords--const,it differ with top-level and low-level,[pointer] right-most const is always top-level;if it‘s top-level,it cannot be changed.Reference types is always low-level.
5.Declarations that use type aliases that represent compound types and const
can yield surprising results.
For instance, typedef char* pstring is differnt with typedef char *pstring.
6.By implication, a variable that uses auto
as its type specifier must have an initializer:
7. always define header guard in .h or .hpp file in case of this file are included twice.
8.subscript of string and vector, subscript is to fetch the value of vector, it cannot add element
9.evaluate/precedence/associate
notify the order of evaluation
10.compound operator
Each compound operator is essentially equivalent to
a = a op b;
with the exception that, when we use the compound assignment, the left-hand operand is evaluated only once. If we use an ordinary assignment, that operand is evaluated twice: once in the expression on the right-hand side and again as the operand on the left hand. In many, perhaps most, contexts this difference is immaterial aside from possible performance consequences.
11.
12.Do not return local object‘s pointers or reference.
13.Name lookup happens before type checking. Local to outter and find the same name of function.
14.
Note
The inline
specification is only a request to the compiler. The compiler may choose to ignore this request.
In general, the inline
mechanism is meant to optimize small, straight-line functions that are called frequently. Many compilers will not inline a recursive function. A 75-line function will almost surely not be expanded inline.
15.In this constructor there is no constructor initializer list, although technically speaking, it would be more correct to say that the constructor initializer list is empty. Even though the constructor initializer list is empty, the members of this object are still initialized before the constructor body is executed.
16.
The only difference between using class
and using struct
to define a class is the default access level.
As a matter of programming style, when we define a class intending for all of its members to be public
, we use struct
. If we intend to have private
members, then we use class
.
c++ primer learning point.1