Type conversion _ exception handling mechanism

Source: Internet
Author: User
Tags first string

Four types of C + + conversions

Static_cast

C + + static type conversion, the implicit type of language can be converted, basically can

// Syntax:  //b not for type

Reinterpret_cast

The pointer type cannot be transformed; The pointer needs to force the type conversion :

Char " Hello " ; int* P2 == reinterpret_cast<int*> (p1);

dynamic_cast

Type recognition for polymorphic time in inheritance, after the subclass object is passed to the parent class pointer, the parent class pointer can be converted to a downward type using dynamic_cast

Dog *pdog = dynamic_cast<dog*> (base); if (Pdog! = NULL)  {
Conversion success;
}
Const_cast
Remove the read-only attribute of the variable (remove the const attribute)
Note: The arguments are readable and writable.
Through the character array is allocated memory space in the heap;
The string that char* points to is a string constant that cannot be changed.
voidPrintbuf (Const Char*p) {Char* P1 =NULL; P1= const_cast<Char*>(P); p1[0] ='Z';//The value of the memory space pointed to by the pointer can be changed by type conversion}   char buf[] = "afdasdf"; // Allocate memory Printbuf (BUF) in the stack ; // OK char *my_p = "fsjljsf"; // character constants, in the global data area, cannot be changed by itself; Printbuf (my_p); // Error   
      • The first string is the memory allocated in the static constant area, then the pointer my_p the memory allocated in the stack, and then points the pointer to the memory block where "ABACD" resides. The pointer holds the address of "FSJLJSF" , where the memory is in the static constant zone and is immutable.
      • Char buf[] array is in the stack, the array stack is allocated memory, is a local variable, that is, because Char buf[] is allocated memory, so here just copy "AFDASDF" to Char buf[] in the memory allocated in the stack, is readable and writable. This is different from the pointer, the pointer is not allocated memory, the pointer points to a static constant area of memory.
Exception handling mechanism:

After an exception occurs, the cross function, from throw directly to the catch
There may be exceptions:

voidDivideintXinty) {if(Y = =0)Throwx;}//test Case:Try{Divide (Ten,0);}Catch(inte) cout<<"10 divided by"<< e <<Endl;//throw;//received exception not handled}

The exception is handled and the program does not terminate. Catch to exception, but continue to throw, by the program itself error handling, or other catch processing

1, if there is an exception through the throw operation creates an exception object and throws;
2. Place the program segment that may throw the exception in the try.
3, the try of the Protection section is not an exception, the try after catch does not execute, until after the try followed by a subsequent catch statement continues to execute.
4. The catch clause executes in the order after the try, capturing or continuing to throw an exception.
5, no match found, function terminate will be automatically called, its default function is called abort termination program;
6, exception processing, through the last catch, using the throw syntax, up still.


Focus:
The constructor has no return type and cannot report the run state by the return value, so it can only be done through a non-functional mechanism.
Namely: exception mechanism, to solve the problem of the constructor error!!!

Exceptions are strictly type-matched and do not implicitly type conversions

Try {  throw'z';} Catch (int" catch int type exception " << Endl;} Catch  " unknown exception " << Endl;}

Conclusion:
1, C + + exception handling mechanism makes exception throwing and exception handling unnecessary in the same function,
The bottom layer is more focused on solving specific problems without having to think too much about exception handling, and upper-level callers can
To set the handling of different types of exceptions in the appropriate location
2, the exception is specifically for the abstract programming of a series of error handling, C + + cannot rely on the function mechanism, because the stack structure
Advanced after out, in turn, access, but exception handling to skip processing, spanning function

Stack spin: (important)
After the exception is thrown, from the start of the try, until the exception is thrown, the period on the stack
All constructed objects are automatically refactored, and the destructor sequence is the opposite of the construction
This process is called a stack spin

The life cycle of an exception variable:

The object is automatically refactored from the try to the exception before it is thrown.
Three types: Int char* class object
Char* is primarily a constant string () global data area
throw (no argument) constructor
Throw Text (); To add ()
Use elements to pick up catch (Text e)//At this time, call the copy constructor, when the destructor, the first to deconstruct the copied e, and then execute the returned element
Use a reference to pick up catch (Text &e)//the same element
The catch (Text *e)//cannot be followed by a pointer, because the element is thrown, and the exception mechanism is strictly matched by type

Exception hierarchy: Exceptions in inheritance
Class A contains other classes that Judge Len, and the constructor for Class A throws exceptions according to the discussion, that is, the constructors of those classes that are contained.
When these classes use the same base class Size,catch, a reference to Class A is used to produce polymorphism.

Type conversion _ exception handling mechanism

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.