Effective C ++, 3rd edition, item 19: regards class design as Type Design)

Source: Internet
Author: User

Item 19: regard class design as Type Design)

By Scott Meyers

Translator: fatalerror99 (itepub's nirvana)

Release: http://blog.csdn.net/fatalerror99/

In C ++, just like other object-oriented programming languages (Object-Oriented Programming Language), a new type is defined by defining a new class (class ). As a C ++ developer, you spend a lot of time increasing your type system. This means that you are not only a class designer, but alsoTypeDesigner (type designer ). Overload functions and operators, Control Memory Allocation and collection, and define object initialization and termination processes-all of which are under your control. Therefore, you should put a lot of effort into class design, just like the effort of Language designers in the design of the language's built-in types (built-in type.

Well-designed classes are challenging because well-designed types are challenging. Good types have simple and natural syntaxes, intuitive semantics, and one or more efficient implementations. In C ++, a class definition (class design) that lacks planning cannot achieve any of the above goals. Even the execution features of a class's member functions (member function) may be affected by the way they are declared.

So how can we design efficient classes? First, you must understand your problems. In fact, every class needs you to face the following questions. The answer is usually oriented to the constraints of your design:

  • How should your new type objects be created and destroyed?How these operations will affect the constructors and destructor of your class, as well as the memory allocation and recovery functions (operator new, operator new [], operator delete, and operator Delete [] -- see Chapter 8) design, as long as you write them.
  • What are the differences between object initialization (object initialization) and object assignment (Object assignment?The answer to this question determines the behavior of your constructors and your assignment operators and their differences. This is important for not obfuscation of initialization (initialization) and assignment (assignment), because they are equivalent to different function calls (see item 4 ).
  • What does passed by value mean for your new type (type) objects?Remember, copy constructor (copy constructor) defines how a pass-by-value of type is implemented.
  • What are the conditions for the valid value of your new type?Generally, a combination of only some values is valid for data members of a class. The combinations determine the invariants (invariant) that your class must maintain ). These invariants (constants) determine that you must perform error checks within member functions (member functions), especially your constructors and assignment operators ), and "setter" functions. It may also affect the exceptions (Exceptions) thrown by your function, and the exception specifications (exception specifications) of your function (the possibility of using it is very small ).
  • Is your new type suitable for an inheritance graph (inherited graph?If you inherit from an existing classes, you will be constrained by the design of those classes, especially whether their functions are virtual or non-virtual) (See items 34 and 36 ). If you want to allow other classes to inherit from your class, it will affect whether the declared function is virtual, especially your destructor (destructor) (see item 7 ).
  • What type conversions does your new type allow (type conversion )?Your type is in the ocean of other types, so do you want to have some conversions between your type and other types? If you want to allow objects of Type T1Implicitly(Implicit) to convert the Type T2 objects, you can either write a type conversion function (type conversion function) (for example, operator T2) in class T1 ), either write a non-explicit Constructor (non-explicit constructor) in class T2, and they must all be called with a single argument (real parameter. If you want to only allowExplicitConversions (display conversion), you need to write the function to execute this conversion, and you also need to avoid making their type conversion operators (type conversion operator) or non-explicit Constructor (non-explicit constructor) can be called with an argument (real parameter. (As an example of both implicit and explicit conversion functions, see item 15 .)
  • Which operators and functions are meaningful for the new type?The answer to this question determines which functions should be declared for your class. Some of them are member functions (member functions), and others are not (see items and 46 ).
  • Which standard functions should not be accepted?You need to declare those all private (private) (see item 6 ).
  • Which members of your new type can be accessed?This problem can help you determine which members are public, protected, and private. It can also help you decide which classes and/or functions should be friends, and whether a class is nested in another class makes sense.
  • What is your new type "undeclared interface "?For performance (performance), exception safety (exceptional security) (see item 29), and resource usage (resource usage) (such as lock and dynamic memory, what kind of guarantee does it provide? The guarantees you provide in these fields will forcibly constrain your classes implementation.
  • To what extent is your new type universal?Maybe you don't really want to define a new type ). Maybe you want to define an entire type family. If so, you don't need to define a new class, but need to define a new class.Template(Class template ).
  • Is a new type really what you need?If you only want to define a new derived class (derived class), so that you can add some functions to an existing class, maybe you can achieve your goal by simply defining one or more non-member functions (non-member functions) or templates (templates.

It is difficult to answer these questions, so defining efficient classes is challenging. However, if you do, the types (types) generated by user-defined classes (user-defined classes) in C ++ can be at least as useful as built-in types (built-in types, it makes all efforts worthwhile.

Things to remember

  • Class design is type design ). Before defining a new type, make sure that all the questions discussed in this item are taken into account.

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.