+ Reply to Thread
Results 1 to 3 of 3

Thread: What is static_cast in C++?

  1. #1

    What is static_cast in C++?

    Hi,

    What is static_cast in C++?

    Is it different than than below?

    char cIndex = 'a' ;
    int nValue = (int) nIndex ;

    Please help.

  2. #2
    It is one type of explicit conversion.

    when there are two different type and one needs to be converted to another type then we can use static_cast.
    Use it very care-fully. You should not use it for casting different type of classes.

    This is as like C style casting.

    Example:

    Code:
    class CDouble
    {
    } ;
    
    class CInt : public CDouble
    {
    } ;
    
    void UseStaticCast()
    {
    	//OK to use below example.
    	double dData = 10.0 ;
    	int nData = static_cast<int> (dData) ;
    
    	//It is risky to cast below pointers.
    	CDouble objDouble ;
    	CDouble* pDouble = &objDouble ;
    	CInt* pInt = static_cast<CInt *> (pDouble) ;
    }

  3. #3
    Thanks for reply.

+ 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