Effective C + + 5

Source: Internet
Author: User

Assigning a value to an object by default constructor is more than assigning the assignment difference directly at construction time.

Like what

String str ("honey~");

String str; Str= "honey~", the efficiency is not the same.

And then the initialization of the loop,

/*

Class A;

A A;

for (int i=0;i<n;i++)//1 secondary constructs, 1 times destructors, n-Times assigned values.

{a=**;}

*/

Class A;

for (int i=0;i<n;i++)//n structure, n-time destructor

{

A A (* *);

}

In summary, the following situation is good.

Transformation is divided into old-style transformation and new transformation.

Legacy Transformation: (T) expression

New transformation:

1.const_cast<t> (expression) Cast away the constness

Removing the constants of an object

2.dynamic_cast<t> (expression) safe downcasting

The only transition that cannot be achieved with old-fashioned transformation. However, actions that can cost significant operating costs.

3.reinterpret_cast <T> (expression) low-level transformation

Example: Int*->int

4.static_cast<t> (expression) forces an implicit conversion.

Example: Non-const-> const, int->double

Pointer-to-base->pointer-to-derived

void*-> typed pointer

The time to use legacy transformations is to invoke the explicit constructor.

Handle:reference, pointers, iterators.

function does not return a member function that points to a lower access level. Because it will reduce the encapsulation nature.

The IDIOM function returns the inner handle of an object that may cause dangling.

Here's a look at the exception:

The exception security function guarantees:

1. Basic commitment: Abnormal error is possible in any legal state.

2. Strongly guarantee that the exception returns to the previous state.

3. Do not throw guarantee: Do not run out of the anomaly.

such as int f () throw (); Instead of throwing an exception, the exception is thrown, causing a serious error.

The Bad_alloc exception is not successful if the dynamic assignment is unsuccessful.

If a function in a system does not have exception security, then the entire system does not have exception security.

encapsulation, exception security.

inline function

1. Exempt call costs

2. If the function body is too large, it will change the page behavior and reduce the command cache device hit rate.

3. Compare the code length generated by the function ontology and function call.

4. Because inlining is a compile-time behavior, it should be in the header file.

5.template is usually inline, but not all.

This instance is best to prove that constructors and destructors do not inline class Base {public: ... private:string bm1,bm2;}; Class Derived {public:derived () {}//appears to be empty. Private:stirng dm1,dm2,dm3; }; Derived::D erived () {base::base (); try{dm1.string::string (); \ catch (...) {base::~base (); throw;} Try{dm2.string::string (), catch (...) {dm1.string::~string (); Base::~base (); Throw Try{dm3.std::string::string (), catch (...) {dm2.string::~string (); dm1.string::~string (); Base::~base (); Throw } }

Pimpl idiom puts the implementation into a class, defines a smart pointer to the implementation class in the interface class, and achieves a breakdown of the implementation.

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.