Highlights of the effective C + + (IV.)

Source: Internet
Author: User

"Effective C + +" fourth: Design and declaration
  1. Make the interface easy to use correctly, not easy to misuse. An interface consists of a return type, an interface name, and a list of parameters, in order to make the interface easy to use correctly, you need to carefully design the return type, preferably simple, direct, natural. Interface name selection is very important to do simple, expressive, unambiguous. Parameter list formal parameter type need to be careful, if you can prevent illegal input, as far as possible, the formal parameter name to do as much as the interface name of the same standard. Also, keep your naming habits consistent and you'll get the magic.
  2. The design class is like a design type. Users are generally familiar with the type support that is built into the language, so it's best to design your own class as a built-in type, making it easy for users to use. In order to do this, you can ask yourself the following questions:
    1) How are new type objects created and destroyed?
    2) What is the difference between the initialization of an object and the assignment of an object?
    3) If the object of the new type is pass-by-value, what does it mean?
    4) What is the "legal value" of the new type?
    5) Does the new type need to match an inheritance graph?
    6) What kind of conversion does the new type require?
    7) What operators and functions are reasonable for this new type?
    8) What kind of standard function should be dismissed? Need to be rejected or is this statement private?
    9) is the member access rights of the new type reasonable?
    10) What is the new type for the declaration interface? What kind of assurance does it provide for efficiency, exceptional security, and resource utilization?
    11) How generalized is the new type? Do you need a template?
    12) Is this new type really needed?
  3. Prefer to replace Pass-by-value with Pass-by-reference-to-const. The first reason to use a const reference as a reference is security, no object cuts occur, and the second reason is efficient. But the simple type built into the language is also more efficient to pass by value.
  4. When you must return an object, don't be paranoid about returning its reference. Return references for efficiency ... Well, don't think about it. Local variables inside the function are destroyed at the end of the function, how can a reference point to a return pointer? It can be, but who promises to release the memory that the pointer points to, and this method does not always work. Maybe you can use RAII to return a smart pointer. However, it is best to return the object.
  5. Declare the member variable as private. The more encapsulation, the implementation can be behind the scenes to do more hands and feet without affecting the client code, to provide access to the encapsulated member variable interface, to give users more consistent use of style; protected is not more encapsulated than public because it is like public in subclasses. There are only two types of access rights in C + +: encapsulated (private) and not encapsulated.
  6. Rather, replace the member function with Non-member and non-friend. This clause is really not suitable for a few words, what I want to say here is that if you find that a member function simply calls a function of a sequence of public types to complete the function, write it as a non-member and non-friend function. This can lead to greater encapsulation, package elasticity and functional scalability, reducing compilation dependencies. The specific reason is very exciting and will be presented in another blog post.
  7. If all parameters require type conversion, use the Non-member function for this purpose. For example, to overload a multiplication operator (*) for a plural class, if as a member function, then the first parameter of this will not be easy to transform, so for convenience, implement it as a non-member function will make the day easier.
  8. Consider writing a swap function that does not throw an exception. Should implement a member version of the SWAP function, and then implement a non-member version of the swap function, let Non-member version calls member version of the Swap function, in general, this is to Copy-and-swap technology, In order to be exceptionally secure, you should not let the swap function throw an exception.

Highlights of the effective C + + (IV.)

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.