More effective C + + clause 13 captures exception in reference mode

Source: Internet
Author: User

1. It is known FROM clause 12 that if a catch clause catches an exception that is passed by value, the thrown exception is copied two times, which reduces efficiency, and passing the derived class object to the base class object may have a cutting problem, but it is also advantageous to pass by value, when the catch clause re-throws the exception, It can choose whether to throw the exception handled by the catch clause or the original exception, which increases the flexibility (throw;)

2. Passing by the pointer seems to avoid the duplication of the exception (although the pointer is still copied, but the cost of 4 bytes is not high), but note that the pointer is not pointing to a local object, because the local object is destroyed, which requires the pointer to the dynamically allocated memory, but the resulting additional problem is the memory release problem, The user must always be aware of the memory release, which adds to the burden, and the programmer must also know that the pointer is pointing to a dynamically allocated memory.

3. Passing objects by reference requires only one copy at a time, while also avoiding the object's cutting problem when the catch clause is passed in, while also supporting polymorphism (Rtti when virtual function calls), but still cannot avoid cutting problems when re-throw (except using "throw;" Statement

4. Note: "More effective C + +" In this section advocates the use of pass-by-reference, but I think that by reference and by value each has its pros and cons (although the advantages of passing by reference is indeed more), but to pass the pointer or do not use as good.

More effective C + + clause 13 captures exception in reference mode

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.