C ++ Primer Plus study notes 9
C ++ Primer Plus study notes 9
Chapter 4 youyuan, exceptions, and others
<<<
Main content:
1) youyuan class
2) youyuan Method
3) Nested classes
4) exception, try block, and catch block are thrown.
5) exception
6) Identification of running stage types (RTTI)
7) dynamic_cast and typeid
8) static_cast, const_cast, and reiterpret_cast
<<<
1. For the TV method of the Remote object, its prototype can be declared before the Remote class declaration, but it must be defined after the Remote class declaration so that the compiler has enough information to compile the method.
class Tv{ friend class Remote; public: void buzz(Remote & r); ……};class Remote{ friend class Tv: public: void Bool volup(Tv & t) { t.volup(); }};inline void Tv::buzz(Remote & r){ ……}
Since the Remote declaration is behind the TV declaration, you can define Remote: volup () in the class declaration, but the TV: buzz method must be defined outside the TV declaration, place it behind the Remote declaration.
Mutual friends
Another scenario where youyuan needs to be used is that a function needs to access private data of two classes. Logically, such a function should be a member function of each class, but this is impossible, it can be a member of a class and a friend of another class, but sometimes it is more reasonable to use functions as friends of two classes.
Supplement: youyuan allows you to develop more flexible interfaces for classes.
2. Nested classes
In C ++, class declarations can be placed in another class. Classes declared in another class are called nested classes ), it avoids name confusion by providing new type class scopes.
Nesting classes is usually used to help implement another class and avoid name conflicts.
Supplement: A nested class is a class declared in other classes. It helps design such helper classes, that is, implementing other classes, but does not have to be part of a public interface.
class Queue{ //class scope definitions //Node is a nested structure definition local to this class struct Node { Item item; struct Node * nest; };}
Node is actually a nested class.
1) Scope of Nested classes
If the nested class is declared as a private part, only the latter knows about it. This is the case for the preceding Node class, because the default access permission of the class is private. You can use the class that contains it. Classes derived from the class that contains it cannot be used, it cannot be used in the external world.
If the nested class protects the declaration, only the latter knows about it. You can use a class that contains it. Classes derived from the class that contains it can be used, and they cannot be used in the external world.
If the nested class is declared in the public part, you can use it for the class that contains it. Classes derived from the class that contains it can be used, and classes can be used in the external world.
The nested structure and enumeration have the same scope as this.
3. Exceptions
1) call abort
Processing method. If one of the parameters is a negative value of another, call the abort () function.
The typical implementation is to send the message abnormal program termination (program exception termination) to the standard error stream)
2) Return Error Codes
A more flexible way than terminating an exception is to use the return value of the function to identify the problem.
3) exception Mechanism
Exception Handling consists of three parts:
① Raise an exception
The throw statement is actually a jump. The keyword indicates that an exception is thrown, and the subsequent value indicates the exception feature.
② Capture exceptions with processing programs
Catch keyword indicates capture exception
③ Use try Blocks
The try block identifies a code block in which a specific exception may be activated. It is followed by one or more catch blocks.
Supplement: The C ++ exception Mechanism provides a flexible way to handle poor programming events, such as unsuitable values and I/O failures, if an exception is thrown, the currently executed function is terminated and the control is passed to the matched catch Block. The catch Block follows the code in the try block.
4. RTTI
RTTI is a runtime stage Type Recognition
The RTTI feature allows the program to detect the object type. The dynamic_cast operator is used to convert a pointer from a derived class to a base class pointer. Its main purpose is to ensure secure calling of virtual functions.
How RTTI works:
C ++ has three supported RTTI elements,
If possible, the dynamic_cast operator uses a pointer to the base class to generate a pointer to the derived class. Otherwise, the operator returns a 0 -- NULL pointer.
The typeid operator returns a value indicating the object type.
The type_info structure stores information about specific types.
RTTI is only applicable to classes that contain virtual functions.
Generally, if the Type of the object (* pt) pointed to is Type or directly or indirectly derived from Type, the expression is dynamic_cast. (Pt) converts the pointer pt to a Type pointer. Otherwise, the result is 0, that is, a null pointer.