title: Composition: using traits to avoid forwarding functions?
body: <p>Let's say we have two classes, <code>A</code> and <code>B</code>. When using composition to model a <em>"has-a"</em> or <em>"is-implemented-in-terms-of"</em> relationship (e.g. <code>B</code> <em>has-a</em> <code>A</code>), one of the drawbacks vs. inheritance is that <code>B</code> does not the contain public functionality of <code>A</code> that it requires. In order to gain access to <code>A</code>s public functions, it is necessary to provide forwarding functions (as opposed to inheritance, where <code>B</code> would inherit all of <code>A</code>s public functions).</p>
  
  <p>To give a more concrete example, let's say we have a <code>Person</code> which <em>has-a</em> <code>ContactInfo</code>:</p>
  
  <pre><code>using namespace std;
  
  class ContactInfo
  {
  public:
     ContactInfo();
     void updateAddress(string address);
     void updatePhone(string phone);
     void updateEmail(string email);
  private:
     string address;
     string phone;
     string email;
  };
  
  class Person
  {
  public:
     Person();
     // Forwarding functions:
     void updateAddress(string address){contactInfo.updateAddress(address)};
     void updatePhone(string phone){contactInfo.updatePhone(phone)};
     void updateEmail(string email){contactInfo.updateEmail(email)};
  private:
     ContactInfo contactInfo;
  };
  </code></pre>
  
  <p>Ignoring any deficiencies in the design (it is just a contrived example to demonstrate my question below), I have had to tediously replicate the exact function signatures from <code>ContactInfo</code> in <code>Person</code>. In a more complex example there could be many such functions, and many layers of composed classes, leading to much code duplication with all the usual problems of maintenance and being error-prone, etc.</p>
  
  <p>Nonetheless, this is the recommended practice for modelling <em>"has-a"</em> or <em>"is-implemented-in-terms-of"</em> according to sources such as Item 38 of Meyers' Effective C++, and Item 24 of Sutter's Exceptional C++ (<a href="http://www.gotw.ca/publications/mill06.htm" rel="noreferrer">link</a>).</p>
  
  <p>Whilst researching this, I came across <a href="http://en.wikipedia.org/wiki/Composition_over_inheritance" rel="noreferrer">this Wikipedia article</a> which discusses the same topic. <a href="http://en.wikipedia.org/wiki/Composition_over_inheritance#Drawbacks" rel="noreferrer">At the bottom of the article</a>, it suggests the following:</p>
  
  <blockquote>
    <p>One drawback to using composition in place of inheritance is that all
    of the methods being provided by the composed classes must be
    implemented in the derived class, even if they are only forwarding
    methods. [...] This drawback can be avoided by using traits.</p>
  </blockquote>
  
  <p>I am fairly new to the concept of traits and with everything I have read, I am finding it hard to relate to the above statement. My question therefore is: <strong>How would one go about using traits to avoid forwarding functions with composition?</strong> An answer based on my example (<code>Person</code> and <code>ContactInfo</code>) would be ideal.</p>
  
  <p><strong>EDIT:</strong> Just to clarify, in response to some of the answers, I am aware of private inheritance as an alternative to composition for modelling <em>"is-implemented-in-terms-of"</em>. My question is not about that, it is specifically about the meaning of Wikipedia's statement relating to traits. I am not asking for alternatives to composition. I've bolded my question to make it clearer that this is what I'm asking.</p>