FAQ 17.08 How can a class Y get the bits of an existing class X without making Y a kind-of X ?

FAQ 17.08 How can a class Y get the bits of an existing class X without making Y a kind-of X?

There are three alternatives. The preferred solution is normal composition, also known as has-a. But in some cases private inheritance should be used, and in a few cases, protected inheritance should be used.

Here is the class X that will be used in each of the three following examples.

 class X { public:   void f() throw();   void g() throw(); private:   int a_;   float b_; }; 

Here is the C++ syntax for composition (that is, Y has-a X). This is the preferred solution.

 class Y1 { public:   void f() throw(); protected:   X x_;                                              <-- 1 }; void Y1::f() throw() { x_.f(); }                                          <-- 2 

(1) Composition: Y1 is not a kind-of X

(2) Y1 calls member functions in X ("reuse")

Here is the C++ syntax for private inheritance, which is semantically the same as has-a but with an increased ripple effect (changes to the protected: part of X can break the private derived class Y2). This is the second of the three alternatives.

 class Y2 : private X {                               <-- 1 public:   using X::f;                                        <-- 2 }; 

(1) Private inheritance: Y2 is not a kind-of X

(2) Same semantics as above: Calls to Y2::f() end up in X::f()

Here is the C++ syntax for protected inheritance, which is semantically the same as has-a but with an even greater ripple effect than private inheritance (changes to the protected: part of X can break the protected derived class Y3 and can also break any classes derived from Y3). This is the last of the three alternatives.

 class Y3 : protected X {                             <-- 1 public:   using X::f;                                        <-- 2 }; 

(1) Protected inheritance: Y3 is not a kind-of X

(2) Same semantics as above: Calls to Y3::f() end up in X::f()

In all three cases, a Y object has a X object, and users of Y are unaware of any relationship between Y and X. For example, user code will not break if the relationship between Y and X changes or even is eliminated. See FAQ 37.01 for more information on private and protected inheritance.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net