《C++ Primer Plus》第十四章学习笔记

 

159:valarray类是由头文件valarray支持的,它支持诸如将数组中所有元素的值相加以及在数组中找出最大和最小值的操作,且它是一个模板类,具体见http://www.cplusplus.com/reference/std/valarray/valarray/

 

160:Compile-time errors are better than runtime errors.

 

161:当初始化列表包含多个项目时,这些项目被初始化的顺序为它们被声明的顺序,而不是它们再初始化列表中的顺序。如果代码使用一个成员的值作为另一个成员的初始化表达式的一部分时,初始化顺序就非常重要了。

 

162:实现has-a关系的两种方式:

           1、类的数据成员是另一个类的对象,此法被称为包含(containment)

           2、私有或保护继承。

两种方法的主要区别:

           1、包含版本提供了被显示命名的对象成员,而似有继承提供了无名称的子对象成员。

           2、方法2的成员初始化列表使用继承类的构造函数,而不是对象名初始化。

           3、使用包含时将使用对象名来调用方法,而使用似有继承时将使用类名和作用域解析操作符来调用方法。

 

163:在私有继承中,在不进行显式类型转换的情况下,不能将指向派生类的引用或指针赋给基类引用或指针。

通常,应使用包含来建立has-a关系;如果新类需要访问原有类的保护成员,或需要重新定义虚函数,则应使用私有继承。

 

164:不同种类的继承:

 

165:当保护或私有继承时,如果想要基类的一个方法成为派生类的公有方法,则有两种方法:

           1、定义一个使用该方法的派生类方法。

           2、使用using声明函数名 EG:using std::valarray<double>::min

 

166:基类的析构函数如果是纯虚的,则必须提供定义,即使是空的。

 

167:Multiple Inheritance的两个主要问题:

           1、从两个不同的基类继承同名方法;(使用”::”,更好的办法是重新定义)

           2、从两个或更多相关基类那里继承同一个类的多个实例。(用虚基类)

 

168:关于虚基类:

• Why the term virtual?

• Why don’t we dispense with declaring base classes virtual and make virtual behavior the norm for MI?

• Are there any catches?

 

First, why the term virtual? After all, there doesn’t seem to be an obvious connection between the concepts of virtual functions and virtual base classes. It turns out that there is strong pressure from the C++ community to resist the introduction of new keywords. It would be awkward, for example, if a new keyword corresponded to the name of some important function or variable in a major program. So C++ merely recycled the keyword virtual for the new

facility—a bit of keyword overloading.

 

Next, why don’t we dispense with declaring base classes virtual and make virtual behavior the norm for MI? First, there are cases in which you might want multiple copies of a base. Second, making a base class virtual requires that a program do some additional accounting, and you shouldn’t have to pay for that facility if you don’t need it. Third, there are the disadvantages presented in the next paragraph.

 

Finally, are there catches? Yes. Making virtual base classes work requires adjustments to C++ rules, and you have to code some things differently. Also, using virtual base classes may involve changing existing code. For example, adding the SingingWaiter class to the Worker hierarchy requires that you go back and add the virtual keyword to the Singer and Waiter classes.

 

169:使用虚基类时新的构造函数规则:如果类有间接虚基类(即非相邻的),除非只需使用该虚基类的默认构造函数,否则必须显式地调用改虚基类的某个构造函数。

 

170:混合使用虚基类和非虚基类:该类将包含一个表示所有虚拟途径的基类子对象和分别表示各条非虚拟途径的多个基类子对象。

 

171:The main change, and the reason for virtual base classes, is that a class that inherits from one or more instances of a virtual base class inherits just one base class object. Implementing this feature entails other requirements:

• A derived class with an indirect virtual base class should have its constructors invoke the indirect base class constructors directly, which is illegal for indirect nonvirtual base classes.

• Name ambiguity is resolved via the dominance rule.

 

172:Typedef的两个缺点:

           1、每次修改类型时都需要编辑头文件;

           2、不能让typedef同时代表两种不同类型。

 

173:除非编译器实现了新的 export关键字(在每一个模板声明前加上export),否则将模板成员函数放置在一个独立的实现文件中将无法运行。

 

174:One way to use a stack of pointers is to have the calling program provide an array of pointers, with each pointer pointing to a different string.

 

175:假设一个类名为classname。可以:在模板声明或模板函数定义内使用classname,但在类外面,即指定返回类型或使用作用域解析操作符时,必须使用完整的classname<Type>。

 

176:模板头template<class T, int n>,其中第二种参数被称为非类型(non-type)参数,注意点:

           1、它可以是整型,枚举,引用或指针,所以double n是不合法的,double* pn是合法的;

           2、不能修改参数的值,也不能使用参数的地址。所以n++和&n都非法;

           3、用作参数的值必须是常量表达式。

 

177:虽然可以为类模板类型参数提供默认值,但不能为函数模板参数提供默认值。

 

178:Explicit Specialization(用于具体化某个类型的类定义)用法:

template <class T>
class name
{
   ...//details omitted
};

template<>
class name<char *>
{
...//details omitted
};

 

179:总觉得这本书没有把”在模板类里定义友元”讲清楚,关于这些内容自己去查了别的资料。

 

180:http://www.iis.sinica.edu.tw/~kathy/vcstl/templates.htm 讲类模板,思路蛮清晰的。

 

 

书中的错误:

P478 “valarray简介”往下数第13行注释中的”int”应改为”double”;

P513 倒数16行的”pop”应改为”po”;

P519程序 14.18 第20行的”/ 10”应改为”/ 5”;

P523 标题“14.4.7”往上数第二行的”Trio<int, short>”应改为”Trio<int, short, short>”;

P533 “14.6 复习题”往上数第5行的”6”应改为”10”;

 

 

 

 

 

 

 

posted on 2012-02-03 12:30  zyearn  阅读(256)  评论(0编辑  收藏  举报