If this is your first time reading this, you probably haven't signed up yet. Do so now by clicking here.

Members Login

Username:
Password:
Remember me ?      

Advertisement

Recommended Books

Partner Links

Forum - iDevApps » Mac, iPhone, iPad & iPod Development » Mac OS X Programming » Type casts for pointers C++ style?

Mac OS X Programming Questions and tips on developing Mac OS X applications, as well as porting.

Reply
Old 2002.07.25, 01:42 AM   #1
Mars_999
Guest
 
Posts: n/a
Default Type casts for pointers C++ style?

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?

Code:
//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
  Reply With Quote
Old 2002.07.25, 02:18 AM   #2
OneSadCookie
Level 7
 
OneSadCookie's Avatar
 
Join Date: Feb 2005
Location: Wellington, New Zealand
Posts: 2,153
Default

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 is offline   Reply With Quote
Old 2002.07.25, 05:58 AM   #3
OneSadCookie
Level 7
 
OneSadCookie's Avatar
 
Join Date: Feb 2005
Location: Wellington, New Zealand
Posts: 2,153
Default

... 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 is offline   Reply With Quote
Old 2002.07.25, 07:15 PM   #4
OneSadCookie
Level 7
 
OneSadCookie's Avatar
 
Join Date: Feb 2005
Location: Wellington, New Zealand
Posts: 2,153
Default

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?
OneSadCookie is offline   Reply With Quote
Old 2002.07.25, 07:20 PM   #5
inio
Guest
 
Posts: n/a
Default

Quote:
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
  Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 06:55 AM.