More Group Sites
School Rankings
Jobless Net
Better Home
Enviro++


Help | Subscribe/Unsubscribe | Rules | Other Group Sites: Better Education | Better Education Forum
Welcome Guest Search | Active Topics | Members | Log In | Register

Compare C++ cast operators Options · View
hong
Posted: Tuesday, August 25, 2009 4:56:35 PM

Rank: Administration
Groups: Administration

Joined: 11/23/2008
Posts: 329
Points: 693
Location: Australia
Compare C++ cast operators
Old-style casting, unsafe
- (typename)expression
- typename(expression)

New cast operators, varies
- dynamic_cast<type>(expression)
- const_cast<type>(expression)
- static_cast<type>(expression)
- reinterpret_cast<type>(expression)


dynamic_cast
Code:
dynamic_cast<T>(expression)

- Used only on pointer or reference types
- Can be used for up-cast or down-cast (see also Run Time Type Information RTTI)
- Only for polymorphic types with virtual-functions as these objects have the information about the type: the virtual function table
- Returns null if the cast is unsuccessful

const_cast
Code:
const_cast<T>(expression)

- Is used to remove or add const-ness
- Usually, you should design your variables or methods such that you won’t have to use const_cast
- Failure causes compiler errors

static_cast
- Works where implicit conversion exists
- standard or user-defined conversion
- up-casts
- Safer than (typename)expression or typename(expression)
- e.g. won’t cast int* to float*
- Failure causes a compiler error
- No dynamic checking is done!

reinterpret_cast
- Circumvents the type checking of C++; Very dangerous
- Very implementation-dependent
- Rarely used
Sponsor
Posted: Tuesday, August 25, 2009 4:56:35 PM
jimmy007
Posted: Monday, May 24, 2010 11:08:49 PM
Rank: Advanced Member
Groups: Member

Joined: 3/3/2010
Posts: 3
Points: -8,952
Thanks for topic

Regards
Shubhinetwork
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

ASPNET Theme created by Boskone (Dan Ferguson)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.
This page was generated in 0.174 seconds.