[019] Design class like design type

Source: Internet
Author: User

Objective

When we write a program, no matter what language it is, it always tells you the type that the language should have. We used them directly, but never thought about how these classes were designed!

Fortunately: in OOP, whenever we define a new class, it is equivalent to defining a new type.

So, how is a good type designed? What things do we need to consider?

1, how should objects of the new type be created and destroyed?

Includes constructs and destructors, memory allocations and deallocation functions (operator new, operator Delete,operator new[], operator delete[]), and so on.

2, what is the difference between the initialization of an object and the assignment of an object?

The behavior of constructors and assignment operators and the differences between them.

3, what does it mean if the object of the new type is Pass-by-value (passed by value)?

The copy constructor is used to define how the pass-by-value of a type is implemented.

4, what is the legal value of the new type?

For member variables of class, only certain values are usually valid. Determines that your member functions (especially constructors, assignment operators, and so-called setter functions) must perform error checking.

It also affects the exception that is thrown by the function and the function exception Detail column (Exception specifications)

5, does your new type need to inherit one of the inheritance graph lines?

If you inherit some of the existing classes, you are bound by the design of those classes, especially their function is the effect of virtual or non-virtual. If you allow another class to inherit your class, it will affect whether the function you are declaring (especially the destructor) is virtual.

6, what kind of conversion do you need for your new type?

If you want to allow type T1 to be implicitly converted to type T2, you must write a type conversion function (operator T2) in class T1 or write a constructor for T2 (which can be called by a single argument) in class Non-explicit-one-argument. Or a function that specifically performs a display conversion.

7, what operators and functions are reasonable for this new type?

What functions the class declares.

8, what kind of standard function should be dismissed?

That's exactly what you should declare as private. If you do not want to copy the constructor

9, who should take a member of the new type?

Decide which member is public, protected, private, and decide which classes and/or functions should be friends.

10, what is the "undeclared interface" of the new type?

What are the guarantees for efficiency, exceptional safety, and resource utilization? These guarantees will add the appropriate constraints to your code.

11, how generalized is your new type?

Is it an entire types family, deciding whether to define a class template rather than a class.

12, do you really need a type?

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

[019] Design class like design type

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.