15th friend, exception and other
Friend-Friend class
Table 0-1
Class Tv { Public Friend class Remote; } |
The remote class can use a TV data member, and the remote class is defined after the TV class.
Friend member function
table 0-2
Class Tv; Forward statement Class Remote { Public void Chanup (Tv &t); } Class Tv { Public friend void Remote::set_chan (tv& t, int c); } |
Exception handling
There are three parts to handling exceptions:
- Throw exception
- Using handlers to catch exceptions
- Using try Blocks
Table 0-3
try (Z=hmean (x, y);) catch (Bad_hmean & BG) { BG.MESG (); continue; } catch (Bad_gmean & Hg) { COUT<<HG.MESG (); break; } |
double Hmean (double a,double b) { if (a==-b) throw Bad_hmean (A, b); return 2.0*a*b/(a+b); } |
Double Gmean (double a,double b) { if (a<0| | B<0) Throw Bad_gmean (A, b); Return std::sqrt (A*B); } |
After an exception occurs, returns to the matching catch structure closest to the statement, the automatic variable of the function that jumped out is freed, and the destructor of the class is called.
Exception class
The Stdexcept class inherits from the base class exception class. Defines the Logic_error class and the Runtime_error class, which is a public derivation.
Types of errors in the Logic_error class:
Domain_error: Defining Domain Errors
Invalid_argument: Invalid variable
Length_error: There is not enough space. String has insufficient append operating space
Out_of_bounds: Index Error
Runtime_error class: Errors during runtime
Range_error: The calculated result is not within the range allowed by the function, but is not an overflow or underflow error.
Overflow_error: Overflow error, max value exceeded
Underflow_error: underflow error, less than the minimum non-0 value
RTTI (runtime type identification) run-time type recognition-Applies only to classes that have virtual functions, and only for hierarchies of such classes, the address of the derived object should be assigned to the base class pointer.
dynamic_cast operators: Most commonly used RTTI components
Superb *pm=dynamic_cast<superb *> (PG);
If the type of PG can be safely converted to superb*, if possible, return the address of the object, otherwise, return a null pointer.
Dynamic_cast<type*> (PT); correct, return type*, otherwise, return 0.
typeID makes it possible to determine whether two objects are of the same type and accepts two parameters: the class name and the expression that the result is an object. The return value is a reference to the Type_info object.
typeID (Magnificent) ==typeid (*PG);
A null pointer to PG throws an exception Bad_typeid. Type_info contains the name () function, which returns the class name.
const_cast<type-name> (expression) is converted to const or volatile type, TypeName is the same as expression type
static_cast<type-name> (expression) Type-name can be used when converting to expression or expression to type-name. Enum/integer double/int, cross-transfer.
reinterpret_cast<type-name> (expression)
function pointers cannot be converted to data pointers, pointers cannot be converted to integer or floating-point
C + + Primer Plus Study: 15th chapter