Connect to parta
Lifecycle 7 initialization list
The constructor has a name, a form parameter table, and a function body, for example
Sales_item: sales_item (const string & book): ISBN (book), units_sold (0), revenue (0.0 ){}
It is valid to omit the initialization list and assign values to data members in the constructor. For example, you can change it:
Sales_item::Sales_item(const string &book){ isbn=book; units_sold=0; revenue=0.0;}
Objective 8: two phases of constructor execution
Initialization phase (data members of the class type are always initialized in the initialization phase)
Ordinary computing stage (the computing stage consists of all the statements of the constructor weight)
Listen 9
The only opportunity for initializing a const or a referenced data member is in the constructor initialization list. If Ci is const
Constref: constref (int ii): I (ii), CI (I), RI (ii ){}
You must use the initialization method for any const or reference type members and any member of the class type without the default constructor.
Copy prohibited In P410
To prevent replication, the class must display the declaration that its replication constructor is private, and its friends and Members can still be copied. If it is to be completely disabled, the declaration of the replication constructor is not defined.
P413 three rules
If a class requires destructor, it also requires the value assignment operator and the copy constructor. This is a useful empirical rule, commonly known as the Three rule.
The p431 overload operator must have a class type operand.
Int operator + (INT, INT); // wrong, the built-in integer plus operator cannot be redefined
Sales_item operator + (const sales_item &, const sales_item &); // right, overload Operator
You cannot define additional operators for any built-in types. For example, you cannot define operator + that accepts two array-type operands.
P443 member access operator
The arrow operator must define a class member function. The unreferenced operator does not need to be defined as a member, but it is generally correct to use it as a member.
P447 difference operator prefix and suffix
The Postfix operator function accepts an additional int-type parameter. When a postfix operator is used, the compiler provides 0 as the real parameter of this parameter. The parameter is not required for the normal operation of the suffix operator. Its sole purpose is to distinguish the suffix function from the prefix function.
Definitions of p4s function objects
A class that defines call operators. Its objects are often called function objects, that is, they are objects that act like functions, such
Absint absobj;
Unsigned int UI = absobj (I); // call absint: Operator (INT)
The function call operation must be declared as a member function. A class can define multiple versions of the function call operator, which are differentiated by the number or type of parameters.
class GT_cls{ public: GT_cls(size_t val=0):bound(val){} bool operator()(const string &s){return s.size()>=bound;} private: std::string::size_type bound;};
You can call gt_cls to find more than 6 words.
Cout <count_if (words. Begin (), words. End (), gt_cls (6) <"words 6 characters or longer" <Endl;
P455 conversion Operator
The conversion function must be a member function. The return type cannot be specified, and the parameter table must be empty. For example
Class smallint {public :... // const {return val;} // The conversion operator, which must be placed in the class. The error PRIVATE: STD: size_t val;} must be placed outside the class ;};
At the same time, the conversion operator should be defined as const, because this function generally does not change the object to be converted.
P461 function overload three steps
1. Determine the set of candidate functions: these are functions with the same name as the called Functions
2. Select a viable function: these are candidate functions that match the number and type of parameters in function calls. When selecting a feasible function, if there is a conversion operation, the compiler also determines which conversion operation to match each form parameter.
3. Select the best matching function. Classes are required to convert real parameters into corresponding types. For real parameters and parameters of the class type, the possible conversion sets include class type conversion.
Ambiguity in p462-465 Functions
The best way to avoid ambiguity is. There is only one way to convert one type to another. The best way is to limit the number of conversion operators, especially to a built-in type, there should be only one conversion.
Multiple conversions
Class smallint {public: Operator int () const {return val;} operator double () const {return val;} PRIVATE: STD: size_type val ;}; void compute (INT); void compute (double); void compute (Long Double); smallint Si; compute (SI); // The Binary Attribute is displayed. Int, you can also change it to compute (static_cast <int> (SI) by converting to double ));
Multiple conversion Constructors
Class smallint {public: smallint (Int = 0) ;}; class integral {public: Integral (Int = 0) ;}; void manip (const integral &); void manip (const smallint &); manip (10); // ambiguity. Any class can match a version of manip, change to manip (smallint (10 ));
Operator Ambiguity
Class smallint {smallint (Int = 0); operator int () const {return val;} friend smallint operator + (const smallint &, const smallint &); Private: STD :: size_t Val ;}; smallint S1, S2; smallint S3 = S1 + S2; // use + to operate int I = S3 + 0; // ambiguity, you can convert 0 to the smallint type, or convert S3 to the int type using the int built-in plus operator.
P475 protected
A derived class can only access the protected member of its base class through a derived class object. The derived class has no special access permission to the protected member of its base class object.
P480 Polymorphism
The static and dynamic types of references and pointers can be different. This is the cornerstone of C ++ to support polymorphism.
When a function defined in the base class is called through a base class reference or pointer, you do not know the exact type of the object that executes the function. The object that executes the function may be of the base class type, it may also be of the derived type.
If you call a non-virtual function, the function defined by the base class type is executed no matter what type of the actual object. If you call a virtual function, you can determine which function to call at runtime.
P488 reference conversion is different from the conversion object
You can pass an object of the derived type to a function that wishes to be referenced by the base class, but before the object is passed to the function, the reference is directly bound to this object. In fact, the real parameter is a reference of this object, the object itself is not copied, and the object is still a derived type object.
When you pass a derived class object to a function that wants to accept a base class Object (not a reference), the type of the form parameter is fixed. during compilation and running, the form parameter is a base class object, if the derived type calls such a function, the base class of the object in the derived class is copied to the form parameter.
One is to convert a derived class object to a base class type reference, and the other is to use the derived class object to initialize or assign a value to the base class object.
P494 replication Control
If the derived class defines its own copy constructor, the copy constructor should explicitly use the base class copy constructor to initialize the base class part of the object.
class Base{/*...*/};class Derived:public Base{ Derived(const Derived& d):Base(d) /*...*/{/*...*/};};
If no base (d) exists, the result is to run the default constructor of the base class to initialize the base class part of the object. As a result, the base class part of the derived class is the default value, and the derived member is a copy of another object.
P497 constructor virtual function
If you call a virtual function in a constructor or destructor, the version defined for the constructor or destructor type is run.
P499 Scope
In the scope of a derived class, the derived class members are blocked. The base class members are blocked even if the function prototype is different.
Struct base {int menfcn () ;}; struct derived: Base {int menfcn (INT) ;}; derived D; Base B; B. menfcn (); // call based. menfcn (10); // call derivedd. menfcn (); // error. No matched match exists. The Compiler does not search for d once the name is found to be the same. base: menfcn (); // call base
The functions defined in the derived class Do not overload the functions defined in the base class. When calling a function through the derived class object, the real parameters must match the version defined in the derived class, the base class function is considered only when the derived class does not define this function.
P501 base class calls virtual functions
Call a function through a reference or pointer of the base class type
Struct base {public: Virtual int FCN () ;}; class D1: public base {public: int FCN (INT) ;}; // D1 has two functions, A virtual function in the base, a defined function class D2: Public D1 {public: int FCN (INT); int FCN ();}; // D2 redefines the two inherited functions base bobj; D1 d1obj; d2 d2obj; base * bp1 = & bobj, * bp2 = & d1obj, * bp3 = & d2obj; bp1-> FCN (); // call basebp2-> FCN (); // call basebp3-> FCN (); // call D2
P528 inline function Template
Template <typename T> inline T min (const T &, const T &);
P533 parameter template type
When a function is called, the parameter is replaced by a value. The value type is specified in the template parameter table. For example
Template <class T, size_t n> void array_init (T (& parm) [N]) {for (size_t I = 0; I! = N; ++ I) {parm [I] = 0 ;}} int X [42]; Double Y [10]; array_init (X ); // convert to array_init (INT (&) [42]); array_init (y); // convert to array_init (double (&) [10]);
Specify the length of an array using a non-type parameter
P538 type conversion is limited
In general, real parameters are not converted to match existing instantiation. On the contrary, new instances are generated. In addition to new instances, the compiler only performs two conversions.
1. Const conversion: functions that accept const references or const pointers can be called using non-const object references or pointers, without generating new instantiation. If a function accepts a non-reference type, both the form parameter and the real parameter ignore the const. Whether a const or non-const object is passed to a function that accepts a non-reference type, the same instantiation is used.
2. array or function-to-pointer conversion: If the template parameter is not of the reference type, the regular pointer conversion is applied to the real parameters of the array or function type. Array arguments are used as pointers to their first element, and function arguments are used as pointers to function types.
Ip552 class templates
Statement of three types of friends
1. A normal non-template class or function's membership declaration grants the membership relationship to a class or function explicitly specified
template <class Type> class Bar{ friend class FooBar; friend void fcn();};
2. The membership Declaration of the class template or function template grants access to all instances of the membership.
template <class Type> class Bar{ template <class T> friend class Foo1; template <class T> friend void temp1_fcn1(const T&);};
3. Only enable the membership statement to access a specific instance of the class template or function Template
template <class T> class Foo2;template <class T> void temp1_fcn2(const &T);template <class T> class Bar{ friend class Foo2<char*>; friend void temp1_fcn2<char*>(char * const &);};
P556 member Template
Internal class definition
template <class Type> class Queue{ public: template <class Iter> void assign(Iter,Iter);};
External class definition
template <class T> template <class Iter>void Queue<T>::assign(Iter beg,Iter end){ //...}
When defining an external class, the class template parameters and their own template parameters must be included. The first template parameter table is a class template, and the second template parameter table is a member template.
Features of the p565 function Template
The actual type or actual value of one or more template parameters in this definition is specified.
template <>int compare<const char*>(const char* const &v1,const char* const &v2){ //...}
When the compare version is called, two character pointers are passed to it. The editor calls the special version and calls the generic version for other real parameter types.
Const char * CP1 = "Word", * CP2 = "hi"; int I1, I2; compare (CP1, CP2); // call the special version compare (I1, I2 ); // call the generic version
P571 function matching and function Template
To determine whether a function has both common functions and function templates, follow these steps:
1. Create and select a function set for this function name, including
Any common function with the same name as the called Function
Any function template is instantiated. The template real parameters are inferred and the template real parameters that match the real parameters used in the call are found.
2. Determine which common functions are feasible. Each template instance in the candidate set is feasible.
3. If conversion is required for calling, sort the feasible functions according to the type of conversion. The conversions allowed by calling the template function instance are limited.
If only one function is available, use it.
If the call has ambiguity, remove all function template instances from the viable function set.
4. Rearrange the feasible functions of the de-vertex function template instance
If only one function is available, use it.
Otherwise, the call is meaningless.
P584 exception match
During the search for a matched catch, the catch found does not need to be the catch that matches the exception most. On the contrary, the first catch found can be selected to handle the exception. Except for the following situations, the exception type must match the catch specifier.
The conversion from non-const to const is allowed. The throw of a non-const object can match the catch specified to accept the const reference.
Conversion from a derived type to a base class type is allowed
Converts an array to a pointer to an array type, and converts a function to a pointer to a function type.
P585 re-Throw
Catch can pass an exception to a function at the upper layer of the call chain by throwing it again.
Throw;
The thrown exception is the original exception object rather than catch its own parameter. When the catch parameter is of the base class type, the actual type thrown depends on the dynamic type of the exception object, rather than the static type of the catch parameter. In general, catch can change its shape parameter. After it changes its shape parameter, if an exception is thrown again, the changes will be transmitted only when the exception descriptor is referenced.
Catch (my_error & eobj) {eobj. status = severerr; throw; // The thrown severerr} catch (my_error eobj) {eobj. status = severerr; throw; // The throw is not changed}
P611 search for real parameters
STD: String s; Getline (STD: Cin, S); // call STD: Getline (STD: istream &, const STD: string &)
Functions that accept class type parameters and functions defined in the same namespace as the class itself are visible when class type objects are used as real parameters.
Call Getline in the current scope, including the scope of the call and find the matching function in the names of the CIN and string types.
P623 virtual inheritance
Virtual inheritance is a mechanism in which a class indicates through virtual inheritance the state of the virtual base class that it wants to share. under virtual inheritance, for a given virtual base class, no matter how many times the class appears as a virtual base class in the hierarchy, It inherits only one shared base class sub-object. Shared base class sub-objects are called virtual base classes.
class istream:: public virtual ios{...};class ostream:: virtual public ios{...};class iostream:: public istream,public ostream{...};
P624 virtual Foundation Class Ambiguity
Assume that the member named X is inherited from multiple derived paths.
1. If X represents the same virtual base class member in each path, there is no ambiguity because a single instance that shares the Member
2. If X is a member of the virtual base class in a path and X is a member of the derived class in another path, there is no ambiguity, the priority of a specific derived class instance is higher than that of a shared virtual base class instance.
3. If each inherited path X represents different members of the derived class, direct access to the member is nontrivial.
P625 virtual base class initialization
In virtual derivation, the constructor of the derived class at the lowest layer initializes the virtual base class. Any class that directly or indirectly inherits the virtual base class must also provide its own initialization type for this base class. As long as you can create an independent object of the type of the virtual base class derived class, this class must initialize its own virtual base class.
P647
You can use the dynamic_cast operator to convert the reference or pointer of a base class object to a reference or pointer of another type in the same inheritance level. If the conversion to the pointer type fails, the result is 0, an error occurred while converting to the reference type.
P649
Typeid: What type is an expression?
Typeid (e) // E is an arbitrary expression or type name. If (typeid (* bp) = typeid (* DP )){...}
P653 class member pointer
class Screen{... private: std::string contents;};
The full type of contents is a member of the screen class. Its type is STD: string, which can be changed
String screen: * ps_screen; // defines the pointer to the string Member of the screen class. string screen: * ps_screen = & screen: contents; // initializes the contents address to ps_screen.
P671 extern "C" function pointer
Extern "C" void (* PF) (INT );
When using PF to call a function, it is assumed that a C function is called and the function is compiled.
The pointer of a C function has different types than that of a C ++ function. You Cannot initialize or assign a pointer to a C ++ function, and vice versa.
Void (* pf1) (INT); // C ++ extern "C" void (* pf2) (INT); // cpf1 = pf2; // Error
One thought: It took me half a month to finish reading this c ++ classic entry-level textbook. How can this problem be solved? I feel that it is better to have a basic understanding of C ++ before reading it, because this book does not spend too much ink on inheritance and polymorphism, but directly combines Object-Oriented Programming and generic programming. I think the fourth part should be the most important and most difficult part of this book. It must be the essence of C ++. I have read it once and understood it a bit. I will definitely read it later, you can deepen your impression through practice. As for the fifth part, it is not very useful, except for exceptions. But it is certainly good to know about it. Now that I have read this book, the next book should be "C ++ programming language". Let's look at what kind of level the book can be upgraded to. Let's look forward to it.