473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Ugly Type Conversion

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
3 1249
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Quick Function | last post by:
Hi, I developed a site and used css. I put almost all style information in the css and used a lot of "id=my_css_class" in the html. They is little style specification in the html. I found that on...
7
by: Madhu Gopinathan | last post by:
Hi, I hope this is the right forum for this question. I am extending ICollection to create a Collection Type (say MyCollection) wherein I can control the types of objects being added to the...
5
by: yxq | last post by:
Hello I am using VS.Net 2002, my icons of XP type look ugly, and i have saw the article. i have added the manifest file. Sometime the icons show very good, but sometime the icons look very...
16
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
2
by: Martin v. Löwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version:...
1
by: lovecreatesbeauty | last post by:
There is a warning/(error? I remember it is an error for line 10 on some compilers before. At least on g++, it is an error.) for line 10. I first read a similar example from `Expert C Programming...
5
by: Jim Langston | last post by:
I've been working on a project to map a MySQL database to a C++ class. Well, I actually got it to work, but some if it I just feel is exceptionally ugly. For example, in my operator<< override: ...
8
by: Smithers | last post by:
Are there any important differences between the following two ways to convert to a type?... where 'important differences' means something more profound than a simple syntax preference of the...
23
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.