Pure virtual function with implementation

前端 未结 9 1992
长发绾君心
长发绾君心 2020-11-22 10:10

My basic understanding is that there is no implementation for a pure virtual function, however, I was told there might be implementation for pure virtual function.

相关标签:
9条回答
  • 2020-11-22 11:05

    Yes this is correct. In your example, classes that derive from A inherit both the interface f() and a default implementation. But you force derived classes to implement the method f() (even if it is only to call the default implementation provided by A).

    Scott Meyers discusses this in Effective C++ (2nd Edition) Item #36 Differentiate between inheritance of interface and inheritance of implementation. The item number may have changed in the latest edition.

    0 讨论(0)
  • 2020-11-22 11:05

    Pure virtual functions with or without a body simply mean that the derived types must provide their own implementation.

    Pure virtual function bodies in the base class are useful if your derived classes wants to call your base class implementation.

    0 讨论(0)
  • 2020-11-22 11:13

    The advantage of it is that it forces derived types to still override the method but also provides a default or additive implementation.

    0 讨论(0)
提交回复
热议问题