By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,127 Members | 1,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,127 IT Pros & Developers. It's quick & easy.

Ugly Type Conversion

P: n/a
Dear Group;

Say i have to convert a point of class A to class B. If i use (B
*)pointer_of_class_A it won't pass compilation but if i use (B *)(void
*) pointer_of_class_A it will compile. My question is that is there a
better way to do this? i have tried the c++ style reinterpret_cast
like reinterpret_cast<B>pointer_of_class_A it doesn't work either.

Could someone enlighten me on this?

TIA!
Mar 24 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Lionel Zhou said:
Dear Group;

Say i have to convert a point of class A to class B.
Did you mean structs? C doesn't have C++ classes.
If i use (B
*)pointer_of_class_A it won't pass compilation but if i use (B *)(void
*) pointer_of_class_A it will compile.
If you need the cast, the conversion is almost certainly very very
unwise indeed. I recommend you find a different way to achieve your
objective.
My question is that is there a better way to do this?
Yes. If you need an A, use an A. If you need a B, use a B. If you need
to convert an A to a B, write a function that takes a pointer-to-A and
a pointer-to-B, and populates the one from the other as appropriate.
i have tried the c++ style reinterpret_cast
like reinterpret_cast<B>pointer_of_class_A it doesn't work either.
That's because C doesn't have that syntax.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 24 '07 #2

P: n/a

"Lionel Zhou" <zh****@aim.comwrote in message
Say i have to convert a point of class A to class B. If i use (B
*)pointer_of_class_A it won't pass compilation but if i use (B *)(void *)
pointer_of_class_A it will compile. My question is that is there a better
way to do this? i have tried the c++ style reinterpret_cast like
reinterpret_cast<B>pointer_of_class_A it doesn't work either.
Get rid of classes A and B and join an introductory class in C. That should
solve these horrible problems.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Mar 24 '07 #3

P: n/a
Lionel Zhou <zh****@aim.comwrites:
Say i have to convert a point of class A to class B. If i use (B
*)pointer_of_class_A it won't pass compilation but if i use (B *)(void
*) pointer_of_class_A it will compile. My question is that is there a
better way to do this? i have tried the c++ style reinterpret_cast
like reinterpret_cast<B>pointer_of_class_A it doesn't work either.
C doesn't have classes. comp.lang.c++ is down the hall, second door
on the left, just past the water cooler.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 25 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.