"C + + Primer Plus" Learning note 9

Source: Internet
Author: User

"C + + Primer Plus" Learning note 9

15th friend, exception and other
<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<
Main content:
1) Friend class
2) Friend class method
3) Nested classes
4) Throw exceptions, try blocks, and catch blocks
5) Exception class
6) Implementation phase type identification (RTTI)
7) dynamic_cast and typeID
8) static_cast, const_cast and Reiterpret_cast
<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<< <<<
1. The TV method for remote objects. Its prototype can be declared before the remote class declaration, but must be defined after the remote class declaration. So that the compiler has enough information to compile the method.

classtv{friend classRemote; Public:voidBuzz (Remote & R); ......};classremote{friend classTv: Public:voidBool Volup (Tv & T) {T.volup (); }};inline voidTv::buzz (Remote & R) {...}

Because the remote declaration is behind the TV declaration, Remote::volup () can be defined in the class declaration. However, the Tv::buzz method must be defined outside the TV declaration so that it is behind the remote declaration.


A common friend
It is also a case that the function needs to access two classes of private data, logically, this function should be the member function of each class. But this is not possible, it can be a member of a class, at the same time there is a friend of the class, but sometimes the function as a friend of the two classes more reasonable.
Add: Friend makes it possible to develop more flexible interfaces for classes
2. Nested classes
In C + +, a class declaration can be placed in a class, and a class declared in another class is called a nested class (nested classes). It avoids the confusion of names by providing a new type class scope.
Nesting of classes is generally done to help implement a class. Avoid name collisions.
Supplemental: A nested class is a class declared in another class, and it helps to design this helper class. Implement other classes. But it does not have to be part of a public interface.

 class Queue {// class scope definitions     //Node isA nested structure definition local to This classstruct Node {item item;    struct Node * NEST; };}

node is actually a nested class.


1) Scope of nested classes
If a nested class is declared as a private part, only the latter knows it. This is the case in the previous node class. Because the default access permission for a class is private, it is available to classes that include it, and classes derived from classes that include it cannot be used. The outside world cannot use it either.


Suppose a nested class is a protection sub-declaration. Then only the latter knows it. The class that includes it is able to use it; a class derived from the class that includes it can be used. The outside world cannot use it either.
It is assumed that a nested class is declared as a public part, that it can be used for classes that include it, that classes derived from the class that includes it can be used, and that the outside world can use it.
The scope of nested structures and enumerations is the same as this.


3, Exception
1) calls abort
processing, assuming that one of the parameters has a negative value for the parameter. Call the Abort () function
whose typical implementation is to send a message to the standard error stream abnormal program termination (abort)
2) return error code
A more flexible method than terminating an exception is to use the return value of the function to indicate the problem.
3) Exception mechanism
There are three components of the exception handling
① throws an exception
The throw statement is actually a jump. Keyword represents an exception that is thrown, followed by a value that indicates the exception's characteristics
② catches a handler exception
Catchkeyword represents a catch exception
③ uses a try block
try block to identify the code block in which a particular exception might be activated. It is followed by one or more catch blocks
supplement: C + + The exception mechanism provides a flexible way to handle poorly programmed events, such as unsuitable values, I/O failures, and so on, and throws an exception to terminate the currently running function. Pass control to the matching catch block. The catch block immediately follows the code in the try block.
4, RTTI
RTTI is an execution stage type recognition
rtti feature that allows a program to detect the type of object. The dynamic_cast operator is used to convert a derived class pointer to a base class pointer. Its main purpose is to ensure that virtual functions can be safely called. How
Rtti works:
C + + has 3 supported Rtti elements,
If possible, the dynamic_cast operator will use a pointer to the base class to generate a pointer to the derived class, otherwise the operator returns the 0--null pointer. The
typeID operator returns a value that indicates the type of the object
the Type_info structure stores information about a particular type
Rtti only applies to classes that include virtual functions.


In general, it is assumed that the type of the object (*PT) That is pointed to is type or that is derived directly or indirectly from type. The expression: Dynamic_cast<type *> (PT) Converts the pointer pt to a pointer of type A; The result is 0. A null pointer.


"C + + Primer Plus" Learning note 9

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.