Effective C + + clause 19: Design class like design type

Source: Internet
Author: User

Remember:

    • Before defining a new type, determine that the topic covered by this article has been considered

To design an efficient class, you need to answer the following questions:
1. How are objects created and destroyed?
This is a problem with constructors and destructors, and of course, if you want to have heap allocations, you also need to overload the new, new[], delete, and delete[] operators;

2. What is the difference between object initialization and object assignment?
Initialization is a matter of constructors and copy constructors (when objects have not yet been produced), but the assignment of objects is different because the object to the left of the equals sign already exists.

3. What does the new type mean if it is pass-by-value?
The corresponding copy constructor is called, and it is important to note whether a deep copy is required.

4. What is the legal value of the new type?
To qualify an object's legal value, it is common to limit the range of member variables in the constructor, and beware of unsafe input values!

5. Does your new type need to match an inheritance graph?
is to determine whether the class you are designing needs to be inherited or inherited, a base class or a derived class. If it is a base class, be aware that the base class is allowed to generate objects (whether you need to design abstract classes with pure virtual functions), and to precede the destructors with virtual.

6. What kind of conversion does the new type require?
Mainly for implicit conversions, operator Othertype () const, but often implicit conversions also mean hidden dangers, so design with caution. Also, be careful in the constructor, if you do not want implicit constructs to occur, precede the explicit keyword.

7. What operators and functions are reasonable for this new type?
is what member functions are designed, and which operators are overloaded.

8. What kind of standard function should be dismissed?
is to say which functions are exposed externally, which functions are used internally, this is the function of private,public and protected, protected only in the class with inheritance to play its real power, ordinary class with private and public is enough.

9. Who should take the new type member?
Similar to the one above, is the encapsulation of classes, in some cases, member variables should be private, and in the public function provides access to these member variables get and set functions. If necessary, you can make the yuan, but friends should also use caution, because some compilers support is not very good, on the other hand, friends become a special case of the destruction of encapsulation, will bring security risks.

10. What is an undeclared interface for the new type?
This is not very clear meaning, first put here ...

11. How generalized is your new type?
This involves generic programming, which is the concept of templates, which is a lot more convenient than C + +-it will require the type to be generated automatically by the compiler.

12. Do you really need a new type?

Effective C + + clause 19: Design class like design type

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.