+ Reply to Thread
Results 1 to 2 of 2

Thread: What is typeid in C++?

  1. #1

    What is typeid in C++?

    What is typeid in C++?

    What is its usage?

    How to use it? Please provide some example?

  2. #2
    "typeid" returns a object "type_info".

    type_info can be used to get the name of the type.

    It can also be used to compare types so that you can identify that two different objects are of different type of objects or not.

    Example:

    Code:
    class CBase
    {
    public:
    	virtual ~CBase() {}
    } ;
    
    class CDerived : public CBase
    {
    } ;
    
    void UseTypeInfo()
    {
    	CBase objBase ;
    
    	CDerived objDerived ;
    
    	char szMessage[256] ;
    	sprintf(szMessage, "Class is of type : %s\n", typeid(objBase).name()) ;
    	OutputDebugString(szMessage) ;
    
    	sprintf(szMessage, "Class is of type : %s\n", typeid(objDerived).name()) ;
    	OutputDebugString(szMessage) ;
    
    	CBase *pBase = &objDerived ;
    	if (typeid(*pBase) == typeid(objDerived))
    	{
    		sprintf(szMessage, "pBase and objDerived are same type.\n") ;
    		OutputDebugString(szMessage) ;
    	}
    	else
    	{
    		sprintf(szMessage, "pBase and objDerived are not same type.\n") ;
    		OutputDebugString(szMessage) ;
    	}
    }

+ 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