title: access members in a 'has-a' relationship by other members of composition
body: <p>I have a class (composition) (has-a relationship) as follows:</p>
  
  <p>The <code>class AP</code> is a bigger class that contains object of <code>class A</code>. <code>AP</code> also has a <code>state</code> variable as its member. </p>
  
  <p>Problem: Can I access <code>AP's member 'state'</code> inside other composition members? (i.e. inside object of type <code>A</code>)? </p>
  
  <p>Here is the code</p>
  
  <pre><code>class A {
   int i;
   public:
    void set(int x)
    { i = x; }
  
    int get()
    { return i; }
  
    void process()
    {
      cout &lt;&lt; ::this-&gt;get() &lt;&lt; endl; //// how to access AP's state variable here ?
    }
  };
  
  
  class AP
  {
    A a;
    int state;
  
   public:
    void process() 
    {     a.process();   }
  
    void set()
    { state = 1; }
  
    int get()
    { return state; }
  };
  
  
  
  /// AP contains (or owns an object of type A)
  /// I need to access AP's state value in object of type A (which is ap's member)
  
  int main()
  { 
   AP ap;  //
   ap.set(); // set state value for AP
   cout &lt;&lt; " get " &lt;&lt; ap.get() &lt;&lt; endl;  // get state value for AP
  
  
   ap.process(); /// calls a.process --&gt; this must access ap's state value    
  
  
  return 0;
  }
  </code></pre>
  
  <p>There are solutions possible such as :</p>
  
  <ol>
  <li><p>passing reference to itself and then access the value. </p></li>
  <li><p>just simply pass the value of <code>state</code> in process() function. ( <code>ap.process(state)</code> )</p></li>
  </ol>
  
  <p>But I want to know whether I can directly access member variables of a class in other members which have a composition (has-a)  [ see <code>class A::process()</code> method. </p>