title: C Style cast and C++ static_cast to reference of pointer
body: <p>This is NOT C++11</p>
  
  <p>I'm interested in the 3rd parameter of Microsoft's 
  <a href="https://msdn.microsoft.com/en-us/library/3hcw9z43.aspx" rel="nofollow noreferrer">CMapStringToOb::GetNextAssoc</a>, which has following definition:</p>
  
  <pre><code> void GetNextAssoc(
     POSITION&amp; rNextPosition,
     CString&amp; rKey,
     CObject*&amp; rValue 
  ) const;
  </code></pre>
  
  <p>Then I've got following easy code for testing: two good cases and one case with compiler error.</p>
  
  <pre><code>class CMyObject : public CObject    //in order to use CMapStringToOb
  {
  public:
      CMyObject(CString name_)
          :name(name_)
      {
      }
  
      void SayHello()
      {
          TRACE(_T("hello") + name);
      }
      CString name;   
  };
  
  void main()
  {
      CMapStringToOb myMap;
      myMap.SetAt(_T("a"), new CMyObject(_T("aaa")));
      myMap.SetAt(_T("b"), new CMyObject(_T("bbb")));
      myMap.SetAt(_T("c"), new CMyObject(_T("ccc")));
  
      //good case 1
      POSITION pos = myMap.GetStartPosition();
      while (pos)
      {
          CString s;
          CMyObject* pMine = NULL;
          myMap.GetNextAssoc(pos, s, (CObject*&amp;)pMine);
          if(pMine)
          {
              pMine-&gt;SayHello();
          }
      }
  
      //good case 2
      pos = myMap.GetStartPosition();
      while (pos)
      {
          CString s;
          CObject* pObject = NULL;
          myMap.GetNextAssoc(pos, s, pObject);
          if(pObject)
          {
              CMyObject* pMine = static_cast&lt;CMyObject*&gt;(pObject);
              pMine-&gt;SayHello();
          }
      }
  
      //bad case:
      //can not compile
      //    error C2440: 'static_cast' : cannot convert from 'CMyObject *' to 'CObject *&amp;'    
      //        static_cast and safe_cast to reference can only be used for valid initializations or for lvalue casts between related classes 
  
      pos = myMap.GetStartPosition();
      while (pos)
      {
          CString s;
          CMyObject* pMine = NULL;
          myMap.GetNextAssoc(pos, s, static_cast&lt;CObject*&amp;&gt;(pMine));  //compile error
          if(pMine)
          {
              pMine-&gt;SayHello();
          }
      }   
  }
  </code></pre>
  
  <p>All I was trying to do is find an proper way to replace the C style casting to C++ style cast in this case.</p>
  
  <p>Reading from <a href="https://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used">this</a>, it mentioned:</p>
  
  <blockquote>
    <p>C casts are casts using (type)object or type(object). A C-style cast
    is defined as the first of the following which succeeds:</p>
  
  <pre><code>const_cast
  static_cast (though ignoring access restrictions)
  static_cast (see above), then const_cast
  reinterpret_cast
  reinterpret_cast, then const_cast
  </code></pre>
  </blockquote>
  
  <p>Q1: Was the above list missing anything (e.g. for rValue)?</p>
  
  <p>Q2: What's the proper way of translate C style cast to C++ style cast in this case ? (good case 2 works, but, is there a more concise one?)</p>
  
  <p>Q3: How is the C Style cast doing for rValue? (in other words, please explain why good case 1 works)</p>