Turning a non-pure virtual function into pure in a subclass

耗尽温柔 提交于 2020-06-14 06:27:10

问题


So, I have this polymorphic hierarchy:

ClassA
Is not abstract, no pure virtual functions, but a few virtual functions

ClassB:public ClassA
Defines an extended interface for a certain type of subclass; 
 is abstract with pure virtual functions

ClassC:public ClassB
Usable class, no more subclassing

Here's the deal, I will have objects of ClassA and ClassC thrown together into containers and iterated through. To perform this iteration, a non-pure virtual function is present in ClassA but is empty with just {}; that is, it is empty, made available only if the iteration encounters a ClassC in which case it is invoked, otherwise it does nothing. I can't have it be pure otherwise I cannot have objects of ClassA.

But to ensure that ClassC does in fact implement that function, forcing the user of that class to do so, I make this function pure virtual in ClassB.

Is this acceptable? Nothing will "break" if I take a non-pure virtual function, make it pure, then make it non-pure again in ClassC ?


回答1:


You're fine if implemented as you present in your explanation. Without going into entire sections on abstract base classes and virtual functions, the standard dictates:

C++ § 10.4p2

An abstract class is a class that can be used only as a base class of some other class; no objects of an abstract class can be created except as subobjects of a class derived from it. A class is abstract if it has at least one pure virtual function. [ Note: Such a function might be inherited: see below. — end note ] A virtual function is specified pure by using a pure-specifier (9.2) in the function declaration in the class definition. A pure virtual function need be defined only if called with, or as if with (12.4), the qualified-id syntax (5.1)

The "below" referenced above leads to this note:

C++11 § 10.4p5

[Note: An abstract class can be derived from a class that is not abstract, and a pure virtual function may override a virtual function which is not pure. - end note]




回答2:


a non-pure virtual function is present in ClassA but not implemented;

That results in linker error. When creating an objects of ClassA or its subclasses, all virtual methods must be implemented. Only for pure virtual methods the body of the method is optional.

But to ensure that ClassC does in fact implement that function, forcing the user of that class to do so, I make this function pure virtual in ClassB.

Yes, this way is correct. But, you should also evaluate that, is it worth having a new class just to make sure that few methods are implemented or not?
For C++11, you can think of making smart use of override identifier as well.

Also, you should not worry about the internal details of vtable, as they are implementation defined.



来源:https://stackoverflow.com/questions/16585033/turning-a-non-pure-virtual-function-into-pure-in-a-subclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!