Link to home
Start Free TrialLog in
Avatar of ASH
ASH

asked on

Only for nietod(question)

Hi:
nietod,would you please tell me what's the difference between
const_cast<> static_cast<> and something like this way,using a parenthese before to cast:
i.e:
int i=30;
char s=(char)i;

would you please give me some examples?
Thanks
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

static_cast is similar to the C-style cast, but slightly less powerful.  It cannot be used to cast away const (like the C-style cast did in the first set of conversions).  It also cannot be used to convert between completely unrelated types.  For example, it cannot convert int* to char* (like C-style did).  However it can be used to convert between related data types.  For example, it can be used to convert from a derived class to a base class--which is no surprise--that can be done automatically, and it can be used to convert from a base class to a derived class, which cannot be done automatically (i.e. without some sort of cast).  For this reason static_cast is most often used for downcasting, that is, for converting from a base class to one of its derived classes.  Now thi conversion is done based on the types used in the cast expression, not on any run time information, so the cast can be dangerious.  That is, it can be used to generate a pointer whose types is a pointer to a derived class even though it actually points to a base class, for example

class B
{
};
class D : public B
{
};

B Bas;
B *BPtr = &Bas;
D *DPtr = static_cast<D *>(BPtr);

The DPtr is initalized to point to Bas.  It is a D* pointer so it allows you to treat B as a D, even though it is not a D, it is a B.

static_cast cannot be used to cast away const and it cannot be used to convert between unrelated types.  So it is safer than a C-style cast.  i.e. it prevents you from accidentally making those sorts of conversions.  However, It does allow you to cast from base to derived when that sort of cast is incorrect though.  (same with C-style).

continues
dynamic_cast is similar to static cast.  it can be used for the same things (conversion between related types) and cannot be used for the same things (removing const, conversion between unrelated types).  The difference is that dynamic cast (when possible) performs the conversion at run-time, not compile time and performs the conversion based on the actually type of the object, not the source type in the expression.  So for example, in the code above where the static cast successfully converted from a pointer to a B object to a pointer to a D object, the dynamic_cast would detect that the object was really a B, not a D and would return a NULL pointer  (if done with references, rather than pointers it throws an exception when an error like this is detected.)  so for example

class B
{
public:
   virtual ~B() {};
};
class D : public B
{
};

B Bas;
D Drv;
B *BPtr1 = &Bas;
B *BPtr2 = &Drv;
D *DPtr` = dynamic_cast<D *>(BPtr1);  // DPtr1 set to NULL;
D *DPtr2 = dynamic_cast<D *>(BPtr2); // DPtr2 set to Drv;

Note that both Bptr1 and BPtr2 have the same type, pointer to a B.  But while BPtr 1 actually points to a B, BPtr2 actually points to a D.  Okay?  Now in the two casts, the compiler has the same information, its sees a downcast from B* to D* which is sometimes legal and sometimes not.  In the first case it detects that the object is actually a B so it doesn't perform the conversion. In the 2nd cast it detects the object is really a D, so it does do the conversion.

Now the "detection of the true types" is possible only if the object contains a virtual function or a virtual base class.  If that is true, the conversion is done dynamically based on the actuall type at run-time.  If that is not true, then the conversion is done at compile type exactly the same as static_cast.

The last type of cast is reinterpret_cast.  Basicaly this is the C-style cast using the new syntax.  It can convert between unrelated types.  Like it can convert any pointer type to any other pointer type (even if the type types are not related.).
hmmm,great explanation:)
One makeup:
virtual base class is for mutil-inheritance to prevent ambiguity.
If two classes both derive from the same base class,and now you derive another one from it.Then it will make ambiguity sometimes:

class Root
{

};

class DerivedOne : public Root
{};
class DerivedTwo : public Root
{};

class Final : public DerivedOne, DerivedTwo
{};


now ,if you have a Final instance and want to upcast it to Root.You will fail because there are two Root(two root address).
The virtue base class is to prevent this:

class DerivedOne : public virtual Root {};
class DerivedTwo : public virtual Root {};

This will make Final has one Root object and you can use it as Root.

Regards
W.Yinan

>> hmmm,great explanation:)
Thanks, but I thought it was kinda of "stream-of-consciousness".  its hard to write well under these circumstances..

I think you expanation missed one little important detail--the reason why you mention it.  The reason, which i touched on very briefly is that a compile time cast (static_cast, reinterpret_cast, C-style cast (even a const_class that convertes types)) might not safely convert for a derived class to a base class in a object that has virtual base clases.  The conversion will be okay if the the object is actually of the source type in the cast expression.  But if the object is really of a type derived from this specified source type, then the cast coudl be performed incorrectly.  i.e

class B {};
class D1 : public virtual B {};
class D2 : public D1 {};

D2 Drv;
D1 D1Ptr = &Drv;
B*BPtr = static_cast<B *>(D1Ptr);

the soucce type is supposed to be a D1, but the object is actually a D2, so this cast might not work correctly.  (Actually, it probably will in this case, but there is no guarantee.)

For this type of situation you must use a dynamic_cast.  It will examine the actual object and figure out how to make the conversion.
Avatar of ASH

ASKER

Adjusted points to 200
Avatar of ASH

ASKER

Dear Wyn, are you fine ?

Hi,nietod.I have increased the points for you excellent answer as Wyn suggest.
I will take a look now.
Avatar of ASH

ASKER

Thanks again.
Maybe I will need clarification later.