The type determines the amount of storage that is allocated for the variable and the set of operations that can be performed on it.
int ival(1024); // direct-init
int ival = 1024; // copy-init
initialization is not assignment
direct syntax is more flexible and can be slightly more efficient
never rely on undefined behavior
no compiler can detect all uses of uninitialized variables
uninitialized variables actually do have a value
in order for multiple files to access the same variable, C++ distinguishes between declarations and definitions
extern: definitioin of a var exists elsewhere in the program. A variable could be declared multiple times in a program, but must be defined only once
A name can be reused as long as it is used in different contexts, from which the different meanings of the name can be distinguished. SCOPE
A name can refer to different entities in different scopes
for (int i = 1; i < 10; i++) ... statement scope only
the definition of local variables HIDEs the global var with the same name, always bad idea to do so
unlike other variables, const variables declared at global scope are local to the file in which the object is defined. The variable exists in that file only and cannot be accessed by other files.
Nonconstant variables are extern by default. Const must explicty specify as extern in order to be accessiable from other files.
constant expression: const int a = 3; const int a = someConst;
when a const is initialized by a value that is not a constant expression, then it should not be defined in header file. Should be in a source file, with extern
the #include facility is a part of the C++ preprocessor, which manipulates the source text of our programs and runs before the compiler.
To avoid name clashes, preprocessor variables usually are written in all uppercase letters.
headers should have guards
angle brackets <> standard header
curly braces {}
DO NOT use namespaces in headers!!!
Although we acn preallocate a given number of elements in a vetor it is usually more efficient to define an empty vector and add elements to it.
An iterator is a type that lets us examine the elements in a container and navigate from one element to another
P260 Introducing This
To understand a member function call, we might think that we we write:
total.same_isbn(trans);it is as if the compiler rewrites the call as:
Sales_item:: same_isbn(&total, trans); //<--- hmm, this is actually the C style code.
// Also reminds me Python object method always have a self ref
In this call, the data member isbn inside same_isbn is bound to the one belonging to total.
-----------
Classes in C++Control what happens when objects are initialized, copied, assigned, and destroyed. In this respect, C++ differs from many other languages, many of which do not give class designers the ability to control these operations.
pointers to const objects:
const double pi = 3.13;
const double *cptr = π // cptr may point to a double that is const, i.e. points to const double. Could point to other const double
pointers that are themselves const:
int errNum = 0;
int *const curErr = &errNum;
typedef string *pstring;
const pstring cstr;
Equals to: string *const cstr; // it is the pointer that is constant
NOT
const string *cstr; // not the object that points to is consant
Declaring a member function with the const keyword specifies that the function is a "read-only" function that does not modify the object for which it is called. A constant member function cannot modify any non-static data members or call any member functions that aren‘t constant.
getters, or avg_price P261
It is a good idea to write constructor initializer in the same order as the members are declared. Moreover, when possible, avoid using members to initialize other members.
C16 Templates and Generic Programming
In C++, templates are the foundation for generic programming. A template is a blueprint or formula for creating a class or a function.
Once the compiler determines the actual template argument(s), it instantiates an distance of the function template for us. Having deduced the actual template arguments, it generates and compiles a version of the function.
C5 Expression
Setting the pointer to 0 after the object it refers to has been deleted makes it clear that the pointer points to no object.
dangling pointer: one that refers to memory that once held an object but does so no longer, cuz it still continas the address of the object to which it pointED (on many machines)
C1 Getting Started
In fact, a primary focus of the design of C++ is to make it possible to define class types that behave as naturally as the built-in type themselves.