+ Reply to Thread
Results 1 to 2 of 2

Thread: Using dynamic_cast in C++

  1. #1

    Using dynamic_cast in C++

    Using dynamic_cast in C++.

  2. #2
    Example:

    Code:
    class CBase
    {
    public:
    	CBase()
    	{
    	}
    
    	virtual ~CBase()
    	{
    	}
    
    	virtual void DisplayMessage()
    	{
    		printf("CBase::DisplayMessage") ;
    	}
    } ;
    	
    class CDerived : public CBase
    {
    public:
    	CDerived()
    	{
    	}
    
    	virtual ~CDerived()
    	{
    	}
    
    	virtual void DisplayMessage()
    	{
    		printf("CDerived::DisplayMessage") ;
    	}
    
    	void DerivedDisplayMessage()
    	{
    		printf("CDerived::DerivedDisplayMessage") ;
    	}
    } ;
    	
    void UseDynamicCast(CBase *pBase)
    {
    	pBase->DisplayMessage() ;
    	//pBase->DerivedDisplayMessage() ; //error C2039: 'DerivedDisplayMessage' : is not a member of 'CBase'
    
    	CDerived *pDerived = dynamic_cast<CDerived *>(pBase) ;
    	pDerived->DerivedDisplayMessage() ;
    }
    
    void UseCasting()
    {
    	CBase *pBase = new CDerived() ;
    	UseDynamicCast(pBase) ;
    }

+ Reply to 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