C Style cast and C ++ static_cast for pointer reference

This is NOT C ++ 11

I'm interested in the third parameter of Microsoft CMapStringToOb :: GetNextAssoc , which has the following definition:

 void GetNextAssoc(
   POSITION& rNextPosition,
   CString& rKey,
   CObject*& rValue 
) const;

Then I have the following simple code to test: two good cases and one case with a compiler error.

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*&)pMine);
        if(pMine)
        {
            pMine->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<CMyObject*>(pObject);
            pMine->SayHello();
        }
    }

    //bad case:
    //can not compile
    //    error C2440: 'static_cast' : cannot convert from 'CMyObject *' to 'CObject *&'    
    //        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<CObject*&>(pMine));  //compile error
        if(pMine)
        {
            pMine->SayHello();
        }
    }   
}

All I was trying to do was find a suitable way to replace the cast of C style with C ++ style in this case.

Counting from this , he mentioned:

C casts are casts using an (type) object or a type (object). A C style summary is defined as the first of the following that succeeds:

const_cast
static_cast (though ignoring access restrictions)
static_cast (see above), then const_cast
reinterpret_cast
reinterpret_cast, then const_cast

Q1: was there anything missing in the above list (e.g. for rValue)?

Q2: C ++ ? ( 2 , ?)

Q3: C rValue? ( , , , 1)

+4
3

static_cast ( ) " ". static_cast CMyObject* CObject*, , . . .

" 2" - .

. : static_cast

+3

- std::map<>. , GetNextAssoc(), . , :

CObject* GetNextAssoc(
    POSITION& rNextPosition,
    CString& rKey,
) const {
    CObject* res = 0;
    GetNextAssoc(rNextPosition, rKey, res);
    return res;
}

, . , dynamic_cast, , CObjects, .

, ? , MFC , , . , ( , ) ( ). .

+1

, :

static_cast<CObject*>(pMine) 

, "CMyObject" "CObject"; ;

static_cast<CMyObject*>(pObject) 

, "CMyObject" "CObject";

static_cast<CObject**>(&pMine) 

FAIL, "CMyObject *" "CObject *";

reinterpret_cast<CObject**>(&pMine) 

- "reinterpret_cast"; ?

:

void CMapStringToOb::GetNextAssoc(
        POSITION& rNextPosition,
        CString& rKey,
        CObject** ppValue)
{
    *ppValue = (the pointer at the current position, point to an instance of "CMyObject");
}

, :

GetNextAssoc(pos, s, reinterpret_cast<CObject**>(&pMine))

, "pMine" "CMyObject" ;

, .

, - (: CYourObject CMyObject)

myMap.SetAt(_T("a"), new CYourObject(_T("aaa")));

GetNextAssoc(pos, s, reinterpret_cast<CObject**>(&pMine));

Compilation time will still be executed, however pMine now points to "CYourObject", which will be UNDEFINED BEHAVIOR at runtime . (static_cast has the same problem though)

+1
source

All Articles