"Effective C + +" study notes-clause 19

Source: Internet
Author: User

*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree ********************************************





Iv. Designs and Declarations



Rule 19:treat class design as type design

Rule 19: Design class is like design type





C + + just like in other OOP (object-oriented programming) languages, when you define a new class, you define a new type. As a C + + programmer, you have a lot of time to extend your classes primarily. It means that you're not just a class designer, it's still a type designer. So you have to be careful with class design.

This article is mainly about how to design an efficient class, and this book provides a series of articles.


? How should objects of the new type be created and destroyed?

This affects the design of your class constructors and destructors, as well as memory allocation functions and deallocation functions.

? What is the difference between the initialization of an object and the assignment of an object?

This determines the behavior of your constructor and assignment operators, as well as the differences between them. In particular, do not confuse "initialization" and "assignment" because they correspond to different function calls.

? What does it mean if the object of the new type is passed by value (passed in the values)?

Remember that the copy constructor is used to define a type of pass-by-value how to implement

? What is the "legal value" of the new type?

For member variables of class, only certain sets of values are usually valid. Those sets of values determine the constraints that your class must maintain, and it determines that your member functions must perform error checking.

? Does your new type need to match an inheritance graph?

If inherited from some of the existing classes, it will be bound by these classes, especially by the function of virtual or non-virtual effect. And if you allow other classes to inherit your class, it will affect the declared function (for example, whether the destructor is virtual)

? What kind of conversion do you need for your new type?

If you want to allow type T1 to be implicitly converted to T2, you must write a type conversion function (operator T2) within class T1 or write a constructor for T2 (which can be called by a single argument) within class non-explicit-one-argument. If you only allow the explicit constructor to exist, write a function that is specifically responsible for performing the conversion, and must not be a type conversion operator or Non-explicit-one-argument constructor.

? What operators and functions are reasonable for this new type?

This decision you will declare for your class which functions, some of which are member functions, some are not.

? What kind of standard function should be dismissed?

Those are the things you have to declare private.

? Who should take a member of the new type?

This will help you decide which member is public, which is protected, and which is private. This also helps you decide which classes or functions should be friend, and whether it is legal to nest them within another.

? What is the "undeclared interface" for the new type (undeclared interface)?

It provides a guarantee of efficiency, exceptional security, and resource utilization, and the guarantees provided in these areas will add the appropriate constraints to your class implementation code.

? How generalized is your new type?

If you are not creating a new type, but a "type family", you should not define a new class, but define a new one.

? Do you really need a new type?

If you simply define a new derived class to add functionality to an existing class, you might be able to define a non-member or templates to achieve the goal.


☆ Please remember ☆

The design of Class is the design of type. Before you define a new type, make sure that you have considered all the issues covered by these terms.




*************************************** Reprint Please specify the Source: Http://blog.csdn.net/lttree********************************************

"Effective C + +" study notes-clause 19

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.