CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2003
    Posts
    938

    Casting:types and advantages

    Hello.
    What are the diffrences between:
    (int)(short variables)
    static_caset <int> short variable
    dynamic cast and other methods.
    Which one should i use and when?
    Thx

  2. #2
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Casting:types and advantages

    static_cast is used for upward or downward casting from a base class to a derived class or vice-versa: when the nature of the object being casted is well-known. dynamic_cast can be used to make Run Time Decisions based on the natureof the object being tested.

    So, if I have a class heirarchy like:
    Code:
    class CBase
       {
       };
       
       class CDerived1: public CBase
       {
       };
        
        class CDerived2: public CBase
        {
        };
    And I create an instance of CDerived1 likes this:
    Code:
     CDerived1 * pDerived1 = new CDerived1 ();
    Then, as I am sure that CDerived1 is also a CBase, I can static cast upwards, like this:
    Code:
    CBase* pBase = static_cast <CDerived1*>(pDerived1);
    Now, given a CBase pointer, how would one know if it belongs to a CDerived1 or a CDerived2 object?

    This is when one uses a dynamic_cast -
    Code:
    CDerived2* pDerived2 = dynamic_cast <CDerived2*> (pBase);
    The dynamic_cast would essentially look-up the Virtual Table, and return a valid pointer if pBase was a CDerived2 object, or NULL otherwise.

    Hence, on dynamic_casting, one would check the pointer before using the instance:
    Code:
    if (pDerived2)  // as returned by dynamic_cast
       {
       	pDerived2->UseD2Methods ();
       }
       else
       {
       	  // pBase did not point to CDerived2: perhaps, to CDerived1
       }
    So, to sum it up: dynamic_cast is a powerful operator that can be used to ascertain the sub-class (CDerived2/CDerived1) given a super-class pointer (CBase*) - hence, it is the casting operator of choice when one needs to downward cast (from base --> derived).

    This type-checking of objects allows us to make run-time decisions, and is called Run Time Type Identification - RTTI.

    A static_cast performs no checks, and when used incorrectly can cause problems - hence, a static_cast is a casting operator of choice when the nature of the object being casted is well known - like in Upwards-Casting (Derived --> Base) as demonstrated above.
    Last edited by Siddhartha; June 9th, 2005 at 09:19 AM.

  3. #3
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    Re: Casting:types and advantages

    Here's Stroustrup's FAQ entry about static_cast:
    http://www.research.att.com/~bs/bs_f...ml#static-cast
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

  4. #4
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Casting:types and advantages

    You will rarely want to do any casting at all.

    - reinterpret_cast might be used when using a C-API including something like a thread function which takes a void *. And when getting a symbol from a library with dlsym (unix) or GetProcAddress (windows).

    - dynamic_cast you should use far less than you might think. The last time I used it was as a second-step from above, so I was doing a reinterpret_cast (inside a class) of the return value of a dlsym to one general base class, and this was then being passed as a parameter to a "builder" object which knew which type it wanted (but this builder object had also been specified at run-time from config information).

    - static_cast you might use to upgrade from char to int (or unsigned char to unsigned int) for example if you are doing low-level bitwise operations with shifts etc.

    - const_cast you might use if a 3rd party library is not const-correct but you know it will not be modifying the object.

    Most of your casting should be hidden away, as well. For example, low-level bitwise operations should normally be in some generic "infrastructure" library, and although the implementation may be using inline functions, you will not be doing these directly at application level.

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Casting:types and advantages

    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured