Q: What are the C++ casting operators?
A: Casting means you change the representation of a variable by changing its type to a different one. In order to type-cast a simple object to another you use the traditional type casting operator. For example, to cast a floating point number of type 'double' to an integer of type 'int':
int i;
double d;
i = (int) d;
or also
This is quite good for basic types that have standard defined conversions, however this operators can also been indiscriminately applied on classes and pointers to classes. ANSI-C++ standard has defined four new casting operators: 'reinterpret_cast', 'static_cast', 'dynamic_cast' and 'const_cast' in order to control these types of conversions between classes...
'reinterpret_cast'
'reinterpret_cast' casts a pointer to any other type of pointer. It also allows casting from pointer to an integer type and vice versa.
This operator can cast pointers between non-related classed. The operation results is a simple binary copy of the value from a pointer to the other. The content pointed does not pass any kind of check nor transformation between types.
In the case that the copy is performed from a pointer to an integer, the interpretation of its content is system dependent and therefore any implementation is non portable. A pointer casted to an integer enough large to fully contain it can be casted back to a valid pointer.
'reinterpret_cast' treats all pointers exactly as traditional type-casting operators do.
'static_cast'
'static_cast' allows to perform any casting that can be implicitly performed as well as also the inverse cast (even if this is not allowed implicitly).
Applied to pointers to classes, that is to say that it allows to cast a pointer of a derived class to its base class (this is a valid conversion that can be implicitly performed) and can also perform the inverse: cast a base class to its derivated class.
In this last case the base class that is being casted is not checked to determine wether this is a complete class of the destination type or not.
'static_cast', aside from manipulating pointers to classes, can also be used to perform conversions explicitly defined in classes, as well as to perform standard conversions between fundamental types:
double d = 3.14159265;
int i = static_cast<int>(d);
'dynamic_cast'
'dynamic_cast' is exclusively used with pointers and references to objects. It allows any type-casting that can be implicitly performed as well as the inverse one when used with polymorphic classes, however, unlike static_cast, dynamic_cast checks, in this last case, if the operation is valid. That is to say, it checks if the casting is going to return a valid complete object of the requested type.
Checking is performed during run-time execution. If the pointer being casted is not a pointer to a valid complete object of the requested type, the value returned is a 'NULL' pointer.
If the type-casting is performed to a reference type and this casting is not possible an exception of type 'bad_cast' is thrown:
'const_cast'
This type of casting manipulates the const attribute of the passed object, either to be set or removed:
Neither of the other three new cast operators can modify the constness of an object.
Notes:
Note: Most of the material in this article is taken from codeguru.com
Coronado enterprises tutorials (formerly Gordon Dodrill's)
You can see sample chapters, but are charged for the full tutorials
http://www.coronadoenterprises.com/
Guru of the week - ie discussion papers on using C++
http://www.cntc.com/resources/gotw.html
Tutorials etc on Borland's CBuilder
http://www.richplum.co.uk/cbuilder/
Tutorial on C for Fortran users
http://www.pottsoft.com/home/c_course/course.html
University lecture on C++
http://m2tech.net/cppclass/
Note on pointers -
perhaps more oriented towards C than C++.
http://www.cudenver.edu/~tgibson/tutorial/
Very simple C under DOS or MS-windows.
Not much C++; possibly useful to someone interested in programming
MS-windows without MFC etc.
http://www.cpp-programming.com
Weekly newsletter on C++
Other things: aimed at helping new and intermediate programmers improve their coding skills.
http://www.cyberelectric.net.au/~collins
www.informit.com
A site run by Macmillan USA containing a lot of information including the several well-known C++ books for free download - if you are prepared to supply name and email address.
http://www.informit.com/
C++ in 21 days - 2nd edition
http://newdata.box.sk/bx/c/
A variety of C++ books on line (Macmillian, Sams, Wiley, IDG etc)
You can see the tables of contents, but you will have to have a subscription to read the books themselves after a free trial.
http://www.itknowledge.com/reference/dir.programminglanguages.c1.html
Elementary introduction to C++ (mostly the C subset)
http://clio.mit.csu.edu.au/TTT/
How to use function-pointers in C and C++, callbacks, functors
http://www.function-pointer.org
http://www.newty.de/fpt/fpt.html
Aimed at people who already have experience with an object-oriented programming language
http://www.entish.org/realquickcpp/
Articles about Win32, C++, MFC articles using VC++ compiler.
http://www.codersource.net
--------------------------------------------------------------------------
Use full Site lists
Google web directory
http://directory.google.com/Top/Computers/Programming/Languages/C%2B%2B/
University of Cambridge Department of engineering
http://www-h.eng.cam.ac.uk/help/tpl/languages/C++.html
Object-Oriented Numeric Web Site
http://oonumerics.org/
http://scicomp.math.uni-augsburg.de/~scicomp/
World-wide-web "C++ Virtual Library"
http://www.desy.de/user/projects/C++.html
Karim Ratib's list of C++ sites (Scientific computing, graphs, GUIs etc)
http://www.IRO.UMontreal.CA/~ratib/code/
Phil Austin's list of oo sites for scientific computing
http://www.geog.ubc.ca/~phil/oo
Manfred Schneider's list of sites (CETUS links)
http://www.objenv.com/cetus/software.html
http://www.rhein-neckar.de/~cetus/software.html
C++ and C SIG (New York)
http://www.cppsig.org/
"Connected Object Solutions" list
http://www.connobj.com/refserv.htm
STL tutorial
Warren Young's list - especially STL
http://www.cyberport.com/~tangent/programming/index.html
Tutorial on the STL by Phil Ottewell.
http://www.yrl.co.uk/~phil/stl/stl.htmlx