zoukankan      html  css  js  c++  java
  • Effective C++ 笔记 —— Item 19: Treat class design as type design.

    How do you design effective classes? First, you must understand the issues you face. Virtually every class requires that you confront the following questions, the answers to which often lead to constraints on your design:

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

    How this is done influences the design of your class's constructors and destructor, as well as its memory allocation and deallocation functions (operator new, operator new[], operator delete, and operator delete[] — see Chapter 8), if you write them.

    How should object initialization differ from object assignment?

    The answer to this question determines the behavior of and the differences between your constructors and your assignment operators. It's important not to confuse initialization with assignment, because they correspond to different function calls (see Item 4).

    What does it mean for objects of your new type to be passed by value?

    Remember, the copy constructor defines how pass-byvalue is implemented for a type.

    What are the restrictions on legal values for your new type?

    Usually, only some combinations of values for a class’s data members are valid. Those combinations determine the invariants your class will have to maintain. The invariants determine the error checking you'll have to do inside your member functions, especially your constructors, assignment operators, and "setter" functions. It may also affect the exceptions your functions throw and,on the off chance you use them, your functions’ exception specifications.

    Does your new type fit into an inheritance graph?

    If you inherit from existing classes, you are constrained by the design of those classes, particularly by whether their functions are virtual or nonvirtual (see Items 34 and 36). If you wish to allow other classes to inherit from your class, that affects whether the functions you declare are virtual, especially your destructor (see Item 7).

    What kind of type conversions are allowed for your new type?

    Your type exists in a sea of other types, so should there be conversions between your type and other types? If you wish to allow objects of type T1 to be implicitly converted into objects of type T2, you will want to write either a type conversion function in class T1 (e.g., operator T2) or a non-explicit constructor in class T2 that can be called with a single argument. If you wish to allow explicit conversions only, you’ll want to write functions to perform the conversions, but you’ll need to avoid making them type conversion operators or non-explicit constructors that can be called with one argument. (For an example of both implicit and explicit conversion functions, see Item 15.)

    What operators and functions make sense for the new type?

    The answer to this question determines which functions you’ll declare for your class. Some functions will be member functions, but some will not (see Items 23, 24, and 46). 

    What standard functions should be disallowed?

    Those are the ones you’ll need to declare private (see Item 6).

    Who should have access to the members of your new type?

    This question helps you determine which members are public, which are protected, and which are private. It also helps you determine which classes and/or functions should be friends, as well as whether it makes sense to nest one class inside another.

    What is the "undeclared interface" of your new type?

    What kind of guarantees does it offer with respect to performance, exception safety (see Item 29), and resource usage (e.g., locks and dynamic memory)? The guarantees you offer in these areas will impose constraints on your class implementation.

    How general is your new type?

    Perhaps you're not really defining a new type. Perhaps you’re defining a whole family of types. If so, you don't want to define a new class, you want to define a new class template.

    Is a new type really what you need?

    If you're defining a new derived class only so you can add functionality to an existing class, perhaps you'd better achieve your goals by simply defining one or more non-member functions or templates.

    Things to Remember:

    • Class design is type design. Before defining a new type, be sure to consider all the issues discussed in this Item.
  • 相关阅读:
    外贸视频教程[外贸人zencart自助建站视频教程]:第一课
    外贸视频教程[外贸人zencart自助建站视频教程]:第二课
    行sqlSQL*PLUS使用(三)
    消息函数windows 程序设计 第三章 (下)
    优化性能[置顶] Android应用性能优化方案
    自定义方法JSP自定义标签
    发票名称<iframe name=document.getElementById("cellFrame").src = "dyszAction!showFpDyMb.do?fpzldm=" + fpzldm;
    纹理寻址DirectX入门 (8) TextureAddressMode
    空间复杂度分段分段有序数组合并成有序(空间复杂度为O(1))
    数据库生成T4模版在代码生成中的应用心得
  • 原文地址:https://www.cnblogs.com/zoneofmine/p/15237361.html
Copyright © 2011-2022 走看看