View Full Version : Type casts for pointers C++ style?
Mars_999
2002.07.25, 01:42 AM
I am used to using C++ style of casting for variables. Now further along I can't figure out how to use that style for pointers?
//C style
int x = 0;
short y = 0;
x = (int)y + 10;
//C++ style
int x = 0;
short y = 0;
x = int(y) + 10;
//now with pointers I am lost??? due to nothing works for me?
//C style
int *a = new int;
short *b = new short;
a = (int*)b;
//C++ style????
Sorry if that seems stupid but I haven't had to do casts on pointers and I like the C++ style better due to it looks like a (). Seems logical to me. =) Thanks
OneSadCookie
2002.07.25, 02:18 AM
C-style casts are still valid for C++ (although Bjarne Stroustrup has expressed wishes that they be disallowed).
The style that you've listed here -- T(x) rather than (T)x -- is technically a construction rather than a cast. If T is a class type, you'll be calling a constructor, and (T)x will be invalid. If T is a primitive, it will be equivalent to a cast.
As you've noticed, you can't use this "constructor" style with all types -- anything that's multiple words, in fact, so unsigned int and float* and const Foo are all out. One possibility is to bracket the type to make the expression unambiguous -- (unsigned int)(x), for example. Depending on your perspective, that may look too much like a C-style cast for your liking.
C++ introduces a number of keywords for casts. The keywords are dynamic_cast, static_cast, reinterpret_cast and const_cast. The syntax for all of them is the same, for example (int)x is equivalent to static_cast<int>(x).
dynamic_cast is used to safely cast from a pointer-to-superclass to a pointer-to-subclass. If the particular instance you're doing it to is not an instance of the subclass, an exception will be thrown.
static_cast is basically equivalent to the same C-style cast; you use it to convert between different types of numbers. It doesn't work with pointers, though.
reinterpret_cast treats the bit pattern of the source as if it were of the destination type. The only thing I've seen this used for is to convert between pointer types: int* foo = new int(); unsigned int* bar = reinterpret_cast<unsigned int*>(foo);. Basically, if you're using this you're probably doing something wrong.
const_cast is used to remove const from a type. For example: const int* foo = &x; *const_cast<int*>(foo) = 3; will work. If you're using this you're definitely doing something wrong :)
OneSadCookie
2002.07.25, 05:58 AM
... and while we're on the subject, does anyone know how to get the MacOSX linker to strip unreachable code? The CW linker did that, but here I am using Apple's tools, my 25KB program to 700KB of libfreetype.a of which I use about 25KB ...
OneSadCookie
2002.07.25, 07:15 PM
OK, just delete the post I was referring to so I make no sense!
Will somebody delete this post & move the immediately preceding post to a new thread?
Originally posted by OneSadCookie
Will somebody delete this post & move the immediately preceding post to a new thread?
You can delete the post yourself. Go into the edit screen, check the box at the top, and hit the button next to it. This morning I woke up and realized the referenced message was both off-topic and inflamitory, so I decided to delete it. Never post 5 minutes before you go to sleep.
mods: if OSC deletes the above two posts, please delete this one. I'll be away for about a week so it'll just confuse people
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd.