+ Reply to Thread
Results 1 to 3 of 3

Thread: What is the use reinterpret_cast?

  1. #1

    What is the use reinterpret_cast?

    When to use reinterpret_cast?

    Is there any application of this keyword?

    Thanks.

  2. #2
    It allows any pointer type to be converted to other pointer type.

    Normally you should avoid use and use other casting operators.

    You should use it whenever you are sure that you will convert back the pointer to its original type.

    Example:

    Code:
    typedef void (*PFNCALLBACK) (void *pContext) ;
    
    class CStudent
    {
    	int m_nRoleID ;
    
    public:
    	CStudent(int nRoleID)
    	{
    		m_nRoleID = nRoleID ;
    	}
    
    	int GetRoleID()
    	{
    		return m_nRoleID ;
    	}
    } ;
    
    void CallBack(void *pContext)
    {
    	CStudent *pStudent = reinterpret_cast<CStudent *> (pContext) ;
    	if (NULL != pStudent)
    	{
    		int nRoleID = pStudent->GetRoleID() ;
    	}
    }
    
    void IAmThirdPartyAPI(PFNCALLBACK pfnCallBack, void *pContext)
    {
    	pfnCallBack(pContext) ;
    }
    
    void UseReinterpretCast()
    {
    	CStudent objStudent(10) ;
    	IAmThirdPartyAPI(CallBack, reinterpret_cast<void *> (&objStudent) ) ;
    }

  3. #3
    Great work man.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts