title: Equivalent to out from C# in C++
body: <p>I want to create a function in C++ to get a pointer to a class with some conditions. I have multiples instances for a class, identified by a number. Depending on number, I want to get the corresponding instance.</p>
  
  <p>In c# it would be something like :</p>
  
  <pre><code>class main
  {
      private Example item1;
      private Example item2;
      private Example item3;
      private Example item4;
  
      public bool InitializeItem(int itemID)
      {
          bool isInitialized = false;
          Example item;
          if (tryGetItem(itemID, out item))
          {
              item = new Example(itemID);
              isInitialized = true;
          }
          return isInitialized;
      }
  
      private bool tryGetItem(int itemID, out Example item)
      {
          bool canGet = false;
          item = null;
          switch (itemID)
          {
              case 1:
                  item = item1;
                  canGet = true;
                  break;
              case 2:
                  item = item2;
                  canGet = true;
                  break;
              case 3:
                  item = item3;
                  canGet = true;
                  break;
              case 4:
                  item = item4;
                  canGet = true;
                  break;
          }
          return canGet;
      }
  }
  
  class Example
  {
      int number { get; set; }
      public Example(int i)
      {
          number = i;
      }
  }
  </code></pre>
  
  <p>But in C++ I'm a little bit confuse with referencing and pointers. I read some tutorials like <a href="https://cpp.developpez.com/cours/cpp/?page=page_6#LVI-C" rel="nofollow noreferrer">this one</a>(in french). I understood basic pointers but with classes and function i'm lost.</p>
  
  <p>With first answer, I changed my code to :</p>
  
  <pre><code>Example item1;
  Example item2;
  Example item3;
  Example item4;
  
  bool tryGetItem(int b, Example &amp;ptr)
  {
      bool canGet = false;
      ptr = NULL;
      switch (b)
      {
      case 1:
          ptr = item1;
          canGet = true;
          break;
          /* etc */
      }
      return canGet;
  }
  
  bool InitializeItem(int id)
  {
      bool isInit = false;
      Example ptr = NULL;
      if (getParam(id, ptr))
      {
          ptr = Example(id);
          isInit = true;
      }
      return isInit;
  }
  </code></pre>
  
  <p>But it doesn't work. I've try to debug, <code>getParam(1, ptr)</code> is true, in the {..} the variable <code>ptr</code> is correctly set to 1 but <code>item1</code> doesn't change.</p>
  
  <p><strong>Edit:</strong>
  I don't think it's the same problem that possible duplicate post. I don't want to modify <code>ptr</code> in <code>tryGetItem</code>, I want to use <code>tryGetItem</code> to make <code>ptr</code> pointing on one of my <code>itemX</code>. After using <code>tryGetItem</code> with value 1, modifying <code>ptr</code> must modify <code>item1</code> too.</p>