[ fromfile: accessderivation.xml id: accessderivation ]
Most of the time, you are likely to see classes using public
derivation, for example
class Square : public Shape { // ... };
public
derivation describes an interface relationship between two classes.
This means that the interface (public
part) of the base class merges with the interface of the derived class.
This is considered an implementation relationship, rather than an is a relationship.
The base class interface (public
part) gets merged with the implementation (private
or protected
, depending on the kind of derivation) of the derived class.
In effect, private
derivation is like adding an extra object as a private
data member to the derived class.
Similarly, protected
derivation is like adding an object as a protected
data member to the derived class, but it shares the this
pointer.
Example 22.10 is a concrete example of a situation in which private
derivation might be appropriate.
The template class Stack
is privately derived from QList.
Example 22.10. src/privatederiv/stack.h
#ifndef _STACK_H_ #define _STACK_H_ #include <QList> template<class T> class Stack : private QList<T> { public: bool isEmpty() const { return QList<T>::isEmpty(); } T pop() { return takeFirst(); } void push(const T& value) { prepend(value); } const T& top() const { return first(); } int size() const { return QList<T>::size(); } void clear() { QList<T>::clear(); } }; #endif
Example 22.11. src/privatederiv/stack-test.cpp
#include "stack.h" #include <QString> #include <qstd.h> using namespace qstd; int main() { Stack<QString> strs; strs.push("hic"); strs.push("haec"); strs.push("hoc"); // strs.removeAt(2); int n = strs.size(); cout << n << " items in stack" << endl; for (int i = 0; i < n; ++i) cout << strs.pop() << endl; }
Error, inherited QList methods are private. |
Suppose you want to derive XStack
, a particular kind of stack, from this Stack
class.
With Stack
privately derived from QList, you will not be able to make use of any QList member functions in the implementation of XStack.
Internally, this enables classes derived from Stack
to make use of the inherited QList protected
interface.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |