#include <iostream>using namespacestd;classA { Public: voidasd () {pri (); }Private: /*Virtual*/ voidpri () {cout<<"base class Functions"<<Endl; }};classD | PublicA {Private: voidPRI ()/*Override*/{cout<<"derived class functions"<<Endl; }};intMainvoid) {B k; A*a = &K; A-ASD (); return 0;}
This code is a place previously not recognized by virtual, and indirectly using pointers or references to call virtual functions or dynamic bindings.
Don't know enough about dynamic bindings.
pointer | reference &virtual.
Article 35: Consider other options for the virtual function
example, Game role Health Index
Review:
1, using the No Virtual Interface (NVI) method, which is a special form of template method design mode, he wrapped in the public non-virtual function of the lower access level (protection and private) functions.
2. Replace the virtual function with a function pointer member variable, which is a decomposition representation of the strategy design pattern.
3. Replace the virtual function with the function<> member variable, thus allowing any callable object to be paired with a demand-compatible signature, which is a form of the strategy design pattern.
4, replace the virtual function of the inheriting system with the virtual function of another inheritance system, which is the traditional implementation method of strategy design pattern.
Please remember
1. The alternative scheme of virtual function includes NVI technique and various forms of strategy design pattern. The NVI technique itself is a special form of template method design mode.
2. One drawback of moving skills from member functions to class external function interfaces is that non-member functions cannot access non-public members of class.
3. function<> behaves like a general function pointer. Such an object can accept all the callable objects of the same type as the given target signature.
Article 36: Never redefine inherited novirtual functions
1, the public inheritance of the derived class is a base class, the base class can adapt to all derived classes is the Novirtual function (the derived class and the base class is executed exactly the same way), the base class should not be modified, otherwise it should be set to the virtual function (derived classes and base class execution differently).
2, such as clause 7 of the Xu destructor The fact is part of the clause.
Article 37: Never redefine inherited default parameter values
1. Never redefine an inherited default parameter value because the default parameter values are static bindings, and the virtual function, the only thing you should overwrite-is dynamic binding.
Effective C + + study notes clause 35-37