|
-
June 9th, 2005, 07:55 AM
#1
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
-
June 9th, 2005, 08:34 AM
#2
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.
-
June 9th, 2005, 09:08 AM
#3
Re: Casting:types and advantages
Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html
-
June 9th, 2005, 09:13 AM
#4
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.
-
June 9th, 2005, 09:14 AM
#5
Re: Casting:types and advantages
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|