title: In what ways can a class access members of another class?
body: <p><a href="https://stackoverflow.com/questions/3288848/initialization-of-object-static-members">Earlier</a>, I asked a question on how to call a static member's member functions so as to initialize it before making actual use of the static object. Then, I realized that I was perhaps making use of the static member in a wrong way, which led to this question:</p>
  
  <p><em>Given a particular class, <code>MyClass</code>, in how many ways can we design our code so that <code>MyClass</code> can gain access to the member functions of another class, <code>YourClass</code>? [N.B. Assume a generic situation where <code>MyClass</code> is declared in <code>MyClass.h</code> and defined in <code>MyClass.cpp</code>, and similarly for <code>YourClass</code>.]</em></p>
  
  <p>I can think of a few, but being far from an expert, I guess you could name several others:</p>
  
  <p><strong>Containment:</strong> This can come in several 'flavors', with direct containment of a <code>YourClass</code> object being one option, while containing a pointer or reference to the object being another option:</p>
  
  <pre><code>class MyClass
  {
  public:
    // Some MyClass members...
  private:
    YourClass instance;  // or YourClass* instance / YourClass&amp; instance;
    // Some other MyClass members...
  };
  </code></pre>
  
  <p><strong>a)</strong> Of course, direct containment is convenient, but I can think of one immediate drawback: if <code>YourClass</code> is a bit hefty in terms of memory requirement, and you have several <code>MyClass</code> instances (as in my linked question), containing the object directly will be out of the question. Besides, the <em>has-a</em> relationship does not always make sense.</p>
  
  <p><strong>b)</strong> Having a pointer or a reference to the object might make better sense in that case. Using a reference has the problem that you might end up referring to an object which does not exist anymore, so you have to make sure that the <code>YourClass</code> object exists for the duration of the existence of the <code>MyClass</code> object.</p>
  
  <p><strong>c)</strong> In the case of a pointer, the problem above still exists, but you can more easily reassign the pointer to a new object.</p>
  
  <p><strong>Inheritance:</strong> One can inherit from the <code>YourClass</code> object, so that the members are inherited, such as:</p>
  
  <pre><code>class MyClass : public YourClass
  {
  public:
    // Some MyClass members...
  private:
    // Some other MyClass members...
  };
  </code></pre>
  
  <p><strong>a)</strong> This is also very simple to set up for a few classes, but may become unwieldy for general use. For example, if <code>YourClass</code> was a random number generator, it wouldn't necessarily make sense to say <code>MyClass</code> <em>is-a</em> random number generator. One can of course define a wrapper class for the random number generator, say call it <code>Randomizable</code> and then <code>MyClass</code> could inherit from <code>Randomizable</code>, which makes good sense.</p>
  
  <p>I would personally like to know more about the pros and cons of static members, global objects, singletons, and how they are correctly used. So, from a 'meta' point of view, what other methods or patterns would work? </p>
  
  <p><em>PS. Though I'm asking from a C++ perspective, I guess the same could be said to apply for many other object oriented languages, so don't worry about giving examples in other languages.</em></p>