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
Printable View
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
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:
And I create an instance of CDerived1 likes this:Code:class CBase
{
};
class CDerived1: public CBase
{
};
class CDerived2: public CBase
{
};
Then, as I am sure that CDerived1 is also a CBase, I can static cast upwards, like this:Code:CDerived1 * pDerived1 = new CDerived1 ();
Now, given a CBase pointer, how would one know if it belongs to a CDerived1 or a CDerived2 object?Code:CBase* pBase = static_cast <CDerived1*>(pDerived1);
This is when one uses a dynamic_cast -
The dynamic_cast would essentially look-up the Virtual Table, and return a valid pointer if pBase was a CDerived2 object, or NULL otherwise.Code:CDerived2* pDerived2 = dynamic_cast <CDerived2*> (pBase);
Hence, on dynamic_casting, one would check the pointer before using the instance:
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).Code:if (pDerived2) // as returned by dynamic_cast
{
pDerived2->UseD2Methods ();
}
else
{
// pBase did not point to CDerived2: perhaps, to CDerived1
}
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.
Here's Stroustrup's FAQ entry about static_cast:
http://www.research.att.com/~bs/bs_f...ml#static-cast
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.
Please see this FAQ: What are c-style casts, static_cast, dynamic_cast etc?.