一个类中将所有的成员函数都尽可能地设置为虚函数总是有益的解决方法

一个类中将所有的成员函数都尽可能地设置为虚函数总是有益的
为什么

------解决方案--------------------
Is that the case? No, that's not all the case.

If every function call is virtual,so you pay the cost of an indirect jump each time you make a function call!

The implementation of virtual functions requires that objects carry information that can be used at runtime to determine which virtual functions should be invoked on the objects.This information typically takes the form of a pointer called a vptr the vptr points to an array of function called vtbl; each class with virtual functions has an associated vtbl. When a virtual function is invoked on an object,the actual function called is determined by following the object's vptr to a vtbl and then looking up the appropriate function pointer in the vtbl!

chew and digest it.

--Over!!
------解决方案--------------------
I'm sorry to make it such a poor format which is too long that hard to read.

Is that the case? No, that 's not all the case. 

If every function call is virtual,
so you pay the cost of an indirect jump each time you make a function call! 

The implementation of virtual functions requires that 
objects carry information that can be used at runtime 
to determine which virtual functions should be invoked on the objects.
This information typically takes the form of a pointer 
called a vptr. 
The vptr points to an array of function called vtbl; 
each class with virtual functions has an associated vtbl. 
When a virtual function is invoked on an object,
the actual function called is determined by following 
the object 's vptr to a vtbl and then 
looking up the appropriate function pointer in the vtbl! 

chew and digest it. 

--Over!!
------解决方案--------------------
这种说法不妥。举两个反例。

第一个例子是MFC。为了减少Virtual Table的使用,MFC的工程师们宁愿用晦涩的宏来组装函数映射,也不愿意从基类继承所有的功能,主要原因就是深层的继承可能造成Virtual Table体积巨大,内存消耗过多。

第二个例子是novtable修饰词。__declspec(novtable)为Microsoft C++扩展的类属性。它会使编译器不产生初始化虚函数表指针和虚函数表的代码,这样一来就减少了生成代码的尺寸。在使用MIDL之前,在接口用novtable是一个不错的主意。

Microsoft都这么尽心竭力避免virtual函数,所以说这种说法不妥,应该具体情况具体分析。
------解决方案--------------------
这个说法……,简直就是胡说。
如果一个类根本不想让其他人继承,为什么要把所有成员函数都设置为虚的?更何况虚函数是需要付出代价的。
这个说法就是无原则的鼓励继承,甚至可以说鼓励滥用继承。要知道继承是C++中第二紧密的耦合关系,仅次于友元。而软件工程的一条明智的原则就是尽量减少耦合。在组合和继承都可以使用的情况下,组合是优先于继承的。
如果你确定你的类中的某个函数不希望被继承,那就绝对不要写成虚的。