virtual函数:接口必须被继承。
non-virtual:接口和实现都被继承。
public继承情况:Liskov Substitution Principle
Base能用的地方都能用Derived代替。
如函数参数用point或reference传递对象。
public继承的关键:is-a模型。
derived-class的名称会遮掩Base-class的名称。
例:
如果要求Baseclass的mf1不被遮蔽,解决办法:
1.在类public中声明using Base::mf1;
如果private继承,指向继承Base class的1个mf1,则可以利用转交函数
virtual void mf1()
{
Base::mf1();
}
pure virtual function在base中声明,必须在derived中定义,即必须提供自己的版本,否则报错。
pure virtual目的是继承接口。
注意pure virtual也可以给出定义。可以实现即覆写又利用转换函数实现继承。
impure virtual的目的是继承接口和缺省实现,如果不定义默认继承,但derived可以覆写它。
80-20规则可以确定 我们一定要关注20%的代码,因为他们用了80%的时间。
non-virtual 继承接口和强制实现。
1.template method设计模式:
public non-virtual成员函数调用private virtual函数 称为 non-virtual interface (NVI)
non-virtual函数称为外覆器
优点:可以额外做事先和事后工作。
derived class可以重新定义继承private virtual函数。
2.function pointer实现strategy模式
构造函数参数接受一个指针。
3.tr1::function实现strategy模式
如:typedef std::tr1::function<int (const A&)> h;//h为任何可调用物。
//签名代表的函数是接受一个reference指向const A 返回int
//该设计可兼容。
只要h的参数是可以转换为const&A的,返回值能转换为int的像函数的都行。
4.传统strategy模式
将本生的virtual函数变为一个类。
任何派生类都不该重新定义基类的non-virtual函数
virtual为动态绑定,默认参数值为静态绑定。
分享到:
相关推荐
Effective C++跟more Effective c++
电子版的effective c++ 和more effective c++ c++四书五经中介绍的经典书籍
中文版 chm Effective C++ More effective C++
effective c++ & more effective c++ 纯文字版
有人把学习C++的分为两类:看过Effective和没看过Effective的. 6寸 重排版
Effective C++ & More Effective C++.chm
Effective c++.pdf Effective c++.pdf
effective c++ word版 effective c++ word版 effective c++ word版 effective c++ word版 effective c++ word版 effective c++ word版 effective c++ word版 effective c++ word版 effective c++ word版 effective ...
Scott Meyers 的Effective C++ 和More Effective C++ 是此类佼佼,Herb Sutter 的Exceptional C++ 则是後起之秀。 这类书籍的一个共通特色是轻薄短小,并且高密度地纳入作者浸淫於C++/OOP 领域多年而广泛的经验。...
effective C++,第一版和第二版是中文pdf,第三版是英文的pdf
more effective c++ 中文版 pdf 资源来自网络 分享给各位学习c++
作者简介 作者:(美国)迈耶斯(Scott Meyers) 迈耶斯(Scott Meyers),二十多年来,Scott Meyers的Effective C++系列书籍(包括《Effective C++》《More Effective C++》和《Effective STL》)为C++编程语言...
More Effective C++ 35 New Ways to Improve Your Programs and Designs
More Effective C++ 简体中文版(pdf 版).pdf
Effective C++中文第三版,带书签
C++程序员进阶书籍Effective C++和More Effective C++,压缩包内共四本书,为中英文对照版,只有Effective C++中文版为高清影印版,其他三本都是文字版本,绝对清晰
Effective C++和More Effective C++ 高清中文
more Effective c++ 看过Essential这本书的可以看这本
More Effective c++中文版和Effective c++中文版英文版