Ugly Type Conversion

L

Lionel Zhou

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!
 
R

Richard Heathfield

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.
 
M

Malcolm McLean

Lionel Zhou said:
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.
 
K

Keith Thompson

Lionel Zhou said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top