类基础 { 虚拟int doFoo(int)= 0; public: int foo(int); }; int Base :: foo(int n) { //检查n int tmp = doFoo(n); //检查tmp 返回tmp; } 类派生:公共基础 { 虚拟int doFoo(int); }; 所以在这里我们让派生类提供所有功能(你好b / b )当然在Base中做了一些默认的实现)但总是 检查传递给doFoo()并从doFoo()返回的值我们可以做一些保证 。 - Erik Wikstr?mBecause it allows you to extend classes without being afraid that youchange the interface. You could for example provide checks in the base-class so that some invariants always are true and this can''t becircumvented by inheritance.class Base{virtual int doFoo(int) = 0;public:int foo(int);};int Base::foo(int n){// do checks on nint tmp = doFoo(n);// Check tmpreturn tmp;}class Derived : public Base{virtual int doFoo(int);};So here we let the derived class provide all the functionality (youcold of course make some default implementation in Base) but by alwayschecking the values passes to and returned from doFoo() we can makesome guarantees.--Erik Wikstr?m 这篇关于虚拟私有成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-19 15:08
查看更多