<<Effective C++>>笔记4

本文探讨了C++编程中的关键设计原则与实践,包括创建易于正确使用且难以误用的接口、偏好传递常量引用而非值、避免返回指向局部对象的指针或引用等。此外还讨论了类设计时的数据成员私有化、优选非成员非友元函数以及类型转换需求等问题。

Chapter 4 Designs and Declarations

Item 18:1)Good interfaces are easy to usecorrectly and hard to use incorrectly. You should strive for thesecharacteristics in all your interfaces.

       2)Ways to facilitate correct use include consistency in interfaces and behavioral compatibility with build-in types.

       3)Ways to prevent errors include creating new types, restricting operations on types,constraining objects values, and eliminating client resource managementresponsibilities.

       4)tr1::shared_ptr supports custom deleters. This prevents the cross-DLL problem, can be used to automatically unlock mutexes, etc.

Item 19: Class design is type design.Before defining a new type, be sure to consider all the issues discussed inthis Item. 构建类之前要考虑很多方面

Item 20:1)Prefer pass-by-reference-to-constover pass-by-value. It’s typically more efficient and it avoids the slicing problem.

       对于正常的C++程序,传值的地方传const引用总是没错的(是传递不是返回),因为const引用完全能够满足传值的需求。

       string就是个类,而不是内置类型,对于类对象而言当传值的时候所引起的构造、析构(包括基类)的消耗是惊人的。

       传引用时当不修改对象的时候const不可少。传值得时候只是对复制版本的修改而不是对原对象的修改。

       slicing problem 切片问题。就是当派生类对象传递给形参为基类对象的时候,基类对象的构造函数被调用,此时构造的是基类对象。派生类对象中的特性被“切除”了。传值是会导致切片问题的,当传引用的时候就能避免切片问题。

       2)The rule doesn’t apply to built-in types and STL iterator and function object types. For them, pass-by-value is usually appropriate.

       本质上引用是以指针的形式实现的(python的引用也是这个样子)

       buit-intypes 本来就是设计为传值效率好的。因为很小巧。iterator和function object其本质上也是指针。

Item 21:Never return a pointer or reference to a local stack object, a reference to a heap-allocated object, or a pointer or reference to a local static object if there is a chance that more than one such object will be needed.

       对于引用,引用只是个别名而已,只有已经存在的对象才能存在引用,当出现引用的时候我们必须问自己,它的另外一个名字叫什么。

Item 22:1)Declare data members private. It gives clients syntactically uniform access to data, affords fine-grained access control, allows invariants to been forced, and offers class authors implementation flexibility.

       类对象的封装性非常的重要,封装深度与操作不变性,数据访问控制,代码易维护性直接相关。当设计降低类对象的封装性的时候,就要考虑这么做的理由。

       2)protected is no more encapsulated than public.

Item 23: Prefer non-member non-friend functions to member functions. Doing so increases encapsulation, packaging flexibility, and functional extensibility.

       这一条也是为了保护封装性。non-friend functions 很容易理解,有友元的时候就有别的类或者函数能够访问类的私有数据,封装性降低。这里的member function是指可以从其他member function实现的接口。通常的方便函数,就是一个成员函数的组合,完成组合中所有成员函数的功能。

       我们应当精简模块对外的接口,任何可以从其他接口实现出来的接口都是多余的,至少他们不应该出现在同一层次上。对程序每个部分需要了解的知识越少,越有利于正确搞笑的实现。反过来也成立:任何数据都应该被最少的代码所见,这样才能适应数据结构的变化。

       对于方便函数可以用命名空间的形式来实现,不同类型的方便函数都可以有单独的头文件中,但是这些都属于同一个命名空间。要用那种类型的就包括该类型方便函数的头文件就行了。

       这就是STL的组织方式,STL有很多的头文件(<vector><algorithm><memory>,etc),各自定义了一些在std名称空间中的功能。当只使用与vector相关的功能的时候就包含头文件<vector>即可。也因为名称空间可以在不同的文件中实现这个缘故,所以才有这种组织形式。

Item 24: If you need type conversions on all parameters to a function (including the one pointed to by the this pointer), the function must be a non-member.

       与成员函数对立的是非成员函数,而不是友元函数,任何时候能避免使用友元函数的时候都要避免使用友元函数。

Item 25:该条款尚未理解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值