Effective C++学习第十天

条款36:绝不重新定义继承而来的non-virtual函数

          non-virtual函数执行的是静态绑定,在编译器就已经决定,因此对象对用的函数只和指针的类型有关,而与指针所指的对象无关;记住non-virtual函数的性质:不变性凌驾于特异性;

条款37:绝不重新定义继承而来的缺省参数值

         静态类型是指在程序中声明时使用的类型,动态类型是指目前所指对象的类型,动态类型变现为一个对象将会有什么样的行为;

      non-virtual和参数缺省值执行的是静态绑定,virtual执行的是动态绑定,代码分析:

class shape{

public:

   enum shapecolor { red, green, blue };

  virtual void draw(shapecolor color=red) const=0;

};

class rectangle:public shape{

public:

          virtual void draw(shapecolor color=green) const;//糟糕的操作

         ...

}

class circle:public shape{

public:

              virtual void draw(shapecolor color)const;

              ...

};

调用 shape *pr=new rectangle;

        pr->draw( );//执行的代码为rectangle::draw(shape::red):

                          //相当于shape执行缺省,rectangle执行virtual函数,一人一半,神奇的操作;

C++这样设计的原因:运行期效率,如果缺省值是动态绑定,编译器就必须用某种方法在运行期为virtual函数决定适当的参数缺省值,这比目前实现的在编译期决定的机制更慢更加复杂;

条款38:通过复合塑模出has-a或者根据某物实现出

          复合是类型之间的一种关系,当某种类型的对象内含它种类型的对象,就是这种关系;复合有两种意义,复合意味着has-a(有一个)或is-implemented-in-terms-of(根据某物实现出);

         如何区分is-a(是一种)和is-implemented-in-terms-of(根据某物实现出)这两种关系,可以通过public继承中D继承B,D对象也是B对象,反之B对象不是D对象来判断;

条款39:明智而审慎地使用private继承

          private继承意味着implemented-in-terms-of(复合),它只有实现部分被继承,接口部分省略;private继承纯粹是一种实现技术,它在软件设计层面上没有意义,其意义只及于软件实现层面;代码分析:

      class person {...};

      class student:private person{ . . .};

      void eat(const person&p);

      person p;    student s;      eat(p);

      eat(s);//编译不通过

原因:1)private继承中编译器不会自动将一个derived class对象转换为一个base class对象;2)private继承而来的所有成员在derived  class中都会变成private属性,纵使它们在base class中原本是public或者protected;   

       尽可能的多用复合,必要的时候才使用private继承(protected成员和virtual函数牵涉进来的时候,或者继承一个empty class时候可以采用private继承)

       需求:定义一个类B(继承一个类B_B),使得这个类的派生D不能调用B_B的成员函数;

设计1:private继承

class B_B{                                           class D:private B_B{

public:                                                 private: 

       explicit B_B(int tickfrency);                              virtual void ontick( ) const;

       virtual void ontick( )const;

};                                                        };

设计2:继承+复合

class D{

private:

        class widgettimer:public B_B{

         public:   

                           virtual void ontick( )const;

                              ...

        };

    widgwttimer timer;

}

在D内声明一个嵌套式private class,后者以public继承B_B并重新定义ontick,然后放这个类型在D对象内;

选择方案2不选择方案1的原因:1)想阻止D的derived class重新定义ontick函数;2)降低D编译的依存性;

          empty class:没有non_static成员变量,没有virtual函数,没有virtual base class;empty class数据大小并不是零,C++规定凡是独立对象都必须有非零大小(这个约束不针对derived class 中的base class成分,它们是非独立的),因此一个empty class 大小为1(C++规定安插一个char类型到空对象中);

         在继承empty class中,如果选择private继承(EBO,empty base optimization,继承空类),可能造成empty base的最优化;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
内容简介: 有人说C++程序员可以分成两类,读过Effective C++的和没读过的。世界顶级C++大师Scott Meyers成名之作的第三版的确当得起这样的评价。当您读过《Effective C++中文版(第3版改善程序与设计的55个具体做法)》后,就获得了迅速提升自己C++功力的一个契机。   在国际上,本书所引起的反响,波及整个计算机技术出版领域,余音至今未绝。几乎在所有C++书籍的推荐名单上,本书都会位于前三名。作者高超的技术把握力、独特的视角﹑诙谐轻松的写作风格﹑独具匠心的内容组织,都受到极大的推崇和仿效。这种奇特的现象,只能解释为人们对这本书衷心的赞美和推崇。   《Effective C++中文版(第3版改善程序与设计的55个具体做法)》不是读完一遍就可以束之高阁的快餐读物,也不是用以解决手边问题的参考手册,而是需要您去反复阅读体会的,C++是真正程序员的语言,背后有着精深的思想与无与伦比的表达能力,这使得它具有类似宗教般的魅力。希望这本书能够帮助您跨越C++的重重险阻,领略高处才有的壮美风光,做一个成功而快乐的C++程序员。 Effective C++中文版(第3版改善程序与设计的55个具体做法)》一共组织55个准则,每一条准则描述一个编写出更好的C++的方式。每一个条款的背后都有具体范例支撑。第三版有一半以上的篇幅是崭新内容,包括讨论资源管理和模板(templates)运用的两个新章。为反映出现代设计考虑,对第二版论题做了广泛的修订,包括异常(exceptions)、设计模式(design patterns)和多线程(multithreading)。   《Effective C++中文版(第3版改善程序与设计的55个具体做法)》的重要特征包括:   ·高效的 classes、functions、templates 和inheritance hierarchies(继承体系)方面的专家级指导。   ·崭新的 "TR1" 标准程序库功能应用,以及与既有标准程序库组件的比较。   ·洞察 C++和其他语言(例如Java、C#、C)之间的不同。此举有助于那些来自其他语言阵营的开发人员消化吸收 C++ 式的各种解法。 目录: 译序 中英简繁术语对照 目录 序言 致谢 导读 1.让自己习惯C++ 条款01:视C++为一个语言联邦 条款02:尽量以const,enum,inline替换#define 条款03:尽可能使用const 条款04:确定对象被使用前已先被初始化 2.构造/析构/赋值运算 条款05:了解C++默默编写并调用哪些函数 条款06:若不想使用编译器自动成生的函数,就该明确拒绝 条款07:为多态基类声明Virtual析构函数 条款08:别让异常逃离析构函数 条款09:绝不在构造和析构过程中调用Virtual函数 条款10:令Operator=返回一个referenceto this 条款11:在Operator=中处理“自我赋值” 条款12:复制对象时勿忘其每一个成分 3.资源管理 条款13:以对象管理资源 条款14:在资源管理类中小心Coping行为 条款15:在资源管理类中提供对原始资源的访问 条款16:成对使用new和delete对象置入智能指针 条款17:以独立语句将newed对象置入智能指针 4.设计与声明 条款18:让接口容易被正确使用,不易被误用 条款19:设计class犹如设计type 条款20:宁以pass-by-reference-to-const替换Pass-by-value 条款21:必须返回对象时,别妄想返回其reference 条款22: 将成员变量声明为private 条款23: 宁以non-member、non-friend替换member函数 条款24:若有所参数皆需类型转换,请为此采用non-member函数 条款25:考虑写出一个不抛异常的swap函数 5.实现 条款26:尽可能延后变量定义式的出现时间 条款27:尽量少做转型动作 条款28:避免返回handles指向对象内部成分 条款29:为“异常安全”而努力是值得的 条款30:透彻了解inlining的里里外外 条款31:将文件间的编译依存关系降至最低 6.继承与面向对象设计 条款32:确定你的public继承塑模出is-a关系 条款33:避免遮掩继承而来的名称 条款34:区分接口继承和实现继承 条款35:考虚virtual函数以外的其他选择 条款36:绝不重新定义继承而来的non-virtual函数 条款37:绝不重新定义继承而来的缺省参数值 条款38:通过复合塑模出has-a或“根据某物实现出” 条款39:明智而审慎地使用private继承 条款40:明智而审慎地使用private继承 7.模板与泛型编程 8.定制new和delete 9.杂项讨论 A 本书之外 B 新旧版条款对映 索引
译序 vii 中英简繁术语对照 ix 目录 xvii 序言 xxi 致谢 xxiii 导读 1. 让自己习惯c++ accustoming yourself to c++ 条款01:视c++ 为一个语言联邦 view c++ as a federation of languages 条款02:尽量以const, enum, inline替换 #define prefer consts,enums, and inlines to #defines. 条款03:尽可能使用const use const whenever possible. 条款04:确定对象被使用前已先被初始化 make sure that objects are initialized before they're used. 2. 构造/析构/赋值运算 constructors, destructors, and assignment operators 条款05:了解c++ 默默编写并调用哪些函数 know what functions c++ silently writes and calls. 条款06:若不想使用编译器自动生成的函数,就该明确拒绝 explicitly disallow the use of compiler-generated functions you do not want. 条款07:为多态基类声明virtual析构函数 declare destructors virtual in polymorphic base classes. 条款08:别让异常逃离析构函数 prevent exceptions from leaving destructors. 条款09:绝不在构造和析构过程中调用virtual函数 never call virtual functions during construction or destruction. 条款10:令operator= 返回一个reference to *this have assignment operators return a reference to *this. 条款11:在operator= 中处理“自我赋值” handle assignment to self in operator=. 条款12:复制对象时勿忘其每一个成分 copy all parts of an object. 3. 资源管理 resource management 条款13:以对象管理资源 use objects to manage resources. 条款14:在资源管理类中小心coping行为 think carefully about copying behavior in resource-managing classes. 条款15:在资源管理类中提供对原始资源的访问 provide access to raw resources in resource-managing classes. 条款16:成对使用new和delete时要采取相同形式 use the same form in corresponding uses of new and delete. 条款17:以独立语句将newed对象置入智能指针 store newed objects in smart pointers in standalone statements. 4. 设计与声明 designs and declarations 条款18:让接口容易被正确使用,不易被误用 make interfaces easy to use correctly and hard to use incorrectly. 条款19:设计class犹如设计type treat class design as type design. 条款20:宁以pass-by-reference-to-const替换pass-by-value prefer pass-by-reference-to-const to pass-by-value. 条款21:必须返回对象时,别妄想返回其reference don't try to return a reference when you must return an object. 条款22:将成员变量声明为private declare data members private. 条款23:宁以non-member、non-friend替换member函数 prefer non-member non-friend functions to member functions. 条款24:若所有参数皆需类型转换,请为此采用non-member函数 declare non-member functions when type conversions should apply to all parameters. 条款25:考虑写出一个不抛异常的swap函数 consider support for a non-throwing swap. 5. 实现 implementations 条款26:尽可能延后变量定义式的出现时间 postpone variable definitions as long as possible. 条款27:尽量少做转型动作 minimize casting. 条款28:避免返回handles指向对象内部成分 avoid returning “handles” to object internals. 条款29:为“异常安全”而努力是值得的 strive for exception-safe code. 条款30:透彻了解inlining的里里外外 understand the ins and outs of inlining. 条款31:将文件间的编译依存关系降至最低 minimize compilation dependencies between files. 6. 继承与面向对象设计 inheritance and object-oriented design 条款32:确定你的public继承塑模出is-a关系 make sure public inheritance models “is-a.” 条款33:避免遮掩继承而来的名称 avoid hiding inherited names. 条款34:区分接口继承和实现继承 differentiate between inheritance of interface and inheritance of implementation. 条款35:考虑virtual函数以外的其他选择 consider alternatives to virtual functions. 条款36:绝不重新定义继承而来的non-virtual函数 never redefine an inherited non-virtual function. 条款37:绝不重新定义继承而来的缺省参数值 never redefine a function's inherited default parameter value. 条款38:通过复合塑模出has-a或“根据某物实现出” model “has-a” or “is-implemented-in-terms-of” through composition. 条款39:明智而审慎地使用private继承 use private inheritance judiciously. 条款40:明智而审慎地使用多重继承 use multiple inheritance judiciously. 7. 模板与泛型编程 templates and generic programming 条款41:了解隐式接口和编译期多态 understand implicit interfaces and compile-time polymorphism. 条款42:了解typename的双重意义 understand the two meanings of typename. 条款43:学习处理模板化基类内的名称 know how to access names in templatized base classes. 条款44:将与参数无关的代码抽离templates factor parameter-independent code out of templates. 条款45:运用成员函数模板接受所有兼容类型 use member function templates to accept “all compatible types.” 条款46:需要类型转换时请为模板定义非成员函数 define non-member functions inside templates when type conversions are desired. 条款47:请使用traits classes表现类型信息 use traits classes for information about types. 条款48:认识template元编程 be aware of template metaprogramming. 8. 定制new和delete customizing new and delete 条款49:了解new-handler的行为 understand the behavior of the new-handler. 条款50:了解new和delete的合理替换时机 understand when it makes sense to replace new and delete. 条款51:编写new和delete时需固守常规 adhere to convention when writing new and delete. 条款52:写了placement new也要写placement delete write placement delete if you write placement new. 9. 杂项讨论 miscellany 条款53:不要轻忽编译器的警告 pay attention to compiler warnings. 条款54:让自己熟悉包括tr1在内的标准程序库 familiarize yourself with the standard library, including tr1. 条款55:让自己熟悉boost familiarize yourself with boost. a 本书之外 b 新旧版条款对映 索引

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值