More Effective C ++ Reading Notes

Source: Internet
Author: User

When you call a function, the control of the program will eventually return to the function call. However, when you throw an exception, the control will never return to the exception. There are three main differences between passing an object to a function or an object to call a virtual function and throwing an object as an exception. First, the exception object is always copied during transmission. When it is captured by passing the value, the exception object is copied twice. Objects that are passed to functions as parameters do not need to be copied. Second, when an object is thrown as an exception, the former type conversion is less than the latter type when it is passed as a parameter (the former has only two conversion forms ). Finally, the catch clause performs exception type matching in the order they appear in the source code. The first catch with successful type matching will be used for execution. When an object calls a virtual function, the selected function is located in the class that best matches the object type, even if the class is not at the beginning of the source code. [Cpp] # include <iostream> using namespace std; class Base {public: Base () {cout <"constructor" <endl ;}~ Base () {cout <"destructor" <endl ;}base (const Base & m) {cout <"copy constructor" <endl; pInt = m. pInt;} void Message () {cout <"Base: Message" <endl;} private: int pInt ;}; class SubClass: public Base {public: SubClass () {cout <"constructor_sub" <endl ;}~ SubClass () {cout <"destructor_sub" <endl;} SubClass (const SubClass &) {cout <"copy constructor_sub" <endl;} void Message () {cout <"SubClass: Message" <endl;} private :}; int main () {int iTemp = 0; try {throw iTemp;} catch (double d) {cout <"double" <endl;} catch (int I) {cout <"int" <endl;} int * piTemp = NULL; try {throw piTemp;} catch (void * e) {cout <"void *" <e Ndl;} catch (int * I) {cout <"int *" <endl;} SubClass n; try {throw n;} catch (Base & e) {e. message (); // throw e;} catch (SubClass & ex) {e. message ();} Base m; // Base * m = new Base (); try {throw m;} catch (Base e) {cout <"Base" <endl ;}// catch (Base & e) // {// cout <"Base &" <endl; //} // catch (const Base & e) // {// cout <"const Base &" <endl; //} // catch (B Ase * e) // {// e-> Message (); // delete e; //} the first part throw iTemp; output: cout <"int" <endl; corresponds to the second case above. If the exception transfer type is a basic type, implicit conversion cannot be performed. The second part throw piTemp; output: cout <"void *" <endl; corresponds to the second case above, indicating that if an exception throws a pointer, the catch parameter can be implicitly converted to the void * pointer. Part 3 throw n; output: constructorconstructor_subconstructorcopy constructor_subBase: Messagedestructor_subdestructor corresponds to the second case above. It indicates that in exception capture, subclass exceptions can be captured through basic parameters. In the third case above, the capture sequence is the order in which the code appears. Open the comments "// throw; // throw e;" respectively. The running result is indeed the same, which is different from the description in the book. In theory, throw should throw a subclass type exception. The last part is mainly to verify the difference between passing a reference and passing a pointer to an exception parameter. Through the program results, we can see that both the pass value and the transfer reference will be copied and constructed. The difference is that the transfer value will be copied twice. However, no copy constructor is called during pointer passing. The pointer is the same as the function parameter. However, it should be noted that exception handling may not be in the same scope as the exception throw location. When a pointer exception is thrown, exceptions may be exposed, so I will call delete e at the end to release resources.

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.