Effective C + + 4. Design and declaration

Source: Internet
Author: User

Clause 18: Make the interface easy to use correctly, not easy to misuse//1. If a customer attempts to use an interface without getting the behavior that he expects, the code should not be compiled. 2. Ways to promote proper use include interface consistency and compatibility with built-in types of behavior. 3. Methods to prevent misuse include establishing new types, restricting operations on types, constraining object values, and eliminating customer resource management responsibilities. The 4.shared_ptr supports custom filters, which can be conveniently used to manage various resources. Terms 20:pass by reference to const replacing pass by value//1.c++ the underlying operation will implement the reference operation as a pointer. 2. Passing a const reference avoids the problem of being cut when the child object passes the parent object. 3. Passing a const reference can effectively avoid unnecessary copying and destroying operations, and has a performance advantage//4. For built-in types and for STL iterators and Function objects in STL, it is more appropriate for them to pass a value than to pass a reference. For this rule, the VS2010 test found that there is almost no difference between the value of the transfer and the reference, so it is possible to use the universal reference instead of the pass-through value. Clause 22: Declare the member variable as PRIVATE//1. Declare member variables private, which gives the customer access to data consistency, fine-grained access control, guaranteed constraints, and allows the designer of the class to fully achieve resiliency. 2. Remember that protected is not more encapsulated than public. Clause 23: Replace member function//1 with non-member function, non-friend function. The less code can access the private member data of the class, the better the encapsulation of the private member data of the class. The following code: the Clearctestdata () function is declared as a non-member function of the class, which reduces the functions that can access the private members of the class, which improves the encapsulation of the class's data. So when you change the data members of a class, the amount of code that needs to be changed is reduced. Class Ctest{public:ctest (): Value0 (0), value1 (0) {}public:void ClearValue0 () {value0 = 0;} void ClearValue1 () {value1 = 0;} Private:int value0;int value1;}; void Clearctestdata (CTest &test) {test.clearvalue0 (); Test.clearvalue1 ();} 2. The above Clearctestdata () series functions, usually as auxiliary tools for classesprovided, declaring it as a non-member function of a class can reduce the dependency of the compilation. Clause 24: If all parameters may require a type conversion, it is best to declare it as a non-member function of the class//1. The following code: Class ctest{public:ctest (int ntemvalue): value (ntemvalue) {}public: Const CTEST operator * (const ctest& TEM) Const{return ctest (value * tem.value);} The reason here is not to return a reference but to return an object by value is to prevent the reference object from being present. int GetValue () const {return value;} In order for a const object to call this function, it must be declared as a constant member function. Private:int value;}; Const CTest funtest (const ctest &AMP;TEM0, const ctest &tem1) {return CTest (tem0. GetValue () * tem1. GetValue ());} CTest Test0 (1); CTest Test1 = Test0 * 2;//allowed by compiling//ctest Test2 = 2 * test0;//not allowed by compile//reason ctest Test1 = Test0 * 2; can be compiled because an implicit type conversion occurs, converting 2 to CT EST type//the reason ctest Test2 = 2 * TEST0; cannot be compiled because only the arguments are in the parameter list, this parameter can only occur implicitly type conversions. CTest Test2 = Funtest (2, 2);//allow compilation, parameters that require implicit conversion are in the formal parameter list//clause 25: Consider writing out a swap function for a class that does not throw an exception//1.swap is an interesting function, originally part of the STL, and later became the spine of abnormal security, and the possibility to handle self-assignment. 2. When the std::swap is inefficient for a custom type (for example, a pointer is used in this type to point to a memory, so swap should swap pointers instead of the object that the pointer is exchanged for), provide a swap member function, and determine that the function does not throw an exception//3. If you provide a member function swap, you should also provide a non-member function version of Swap to invoke the member function version of swap. 4. Do not attempt to add new members to the STD namespace, whichThe behavior is undefined. 5.std::swaptemplate<class _alloc>void Swap (_vb_reference<_alloc> _left,_vb_reference<_alloc> _ right) {//Swaps _left and _right vector<bool> elementsbool _val = _left;//not _std swap_left = _right;_right = _Val; }//by the above code, STD::SWAP () is often inefficient when the object being exchanged contains a pointer member.

  

Effective C + + 4. Design and declaration

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.