473,320 Members | 1,719 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,320 software developers and data experts.

Casting

Hi,

I remember having read (either here or in c.l.c++.m) that the
C++-style casts cover almost all casts you could do with a C-style cast,
except forone more or less rare case. But whereever I look, there is no
mention of this one case .. actually, I found people saying that *any*
C-style cast can be written as a combination of C++-style casts.

Could someone shed some light on this please?

thanks!
--
jb

(reply address in rot13, unscramble first)
Feb 14 '06 #1
9 1782

Jakob Bieling wrote:
Hi,

I remember having read (either here or in c.l.c++.m) that the
C++-style casts cover almost all casts you could do with a C-style cast,
except forone more or less rare case. But whereever I look, there is no
mention of this one case .. actually, I found people saying that *any*
C-style cast can be written as a combination of C++-style casts.

Could someone shed some light on this please?


A C-style cast will let you convert to an inaccessible base class.

Rather than explain what that means, see posts by Ron Natalie in this
thread (from which I copied that statement).

http://groups.google.co.uk/group/com...052e73591ce2e6

Gavin Deane

Feb 14 '06 #2
* Jakob Bieling:

I remember having read (either here or in c.l.c++.m) that the
C++-style casts cover almost all casts you could do with a C-style cast,
except forone more or less rare case. But whereever I look, there is no
mention of this one case .. actually, I found people saying that *any*
C-style cast can be written as a combination of C++-style casts.

Could someone shed some light on this please?


C-cast that can't be expressed as C++ cast:

(InaccessibleBase*) pObject

C++ cast that can't be expressed as C cast:

dynamic_cast<whatever>

Hth.,

- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 14 '06 #3
Thanks for the replies, this has been what I was looking for :)
--
jb

(reply address in rot13, unscramble first)
Feb 14 '06 #4
On Tue, 14 Feb 2006 10:31:20 +0100, "Jakob Bieling"
<ar****************@rot13.com> wrote in comp.lang.c++:
Hi,

I remember having read (either here or in c.l.c++.m) that the
C++-style casts cover almost all casts you could do with a C-style cast,
except forone more or less rare case. But whereever I look, there is no
mention of this one case .. actually, I found people saying that *any*
C-style cast can be written as a combination of C++-style casts.

Could someone shed some light on this please?

thanks!


There is no C++ cast to convert between a pointer to function and a
pointer to object (including void *). Every C and C++ compiler that I
know of allows this with a C cast:

int func(int x)
{
return x + 1;
}

int main()
{
void *vp = (void *)func;
/* ... */
}

Of course the result is completely undefined, and I am fairly sure
that there is nothing in the C standard that requires a compiler to
accept it, that is aside from the fact that a compiler is never
obliged to accept source containing guaranteed undefined behavior.

But several platforms, including POSIX and Windows, depend on this
behavior and doing "what they expect" when the pointer sizes are the
same.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 15 '06 #5
There is no C++ cast to convert between a pointer to function and a
pointer to object (including void *).

reinterpret_cast ?

I haven't used it in a while, but I used to do all sorts of mad stuff
with it back in the day.

-Tomás
Feb 15 '06 #6
Tomás <NU**@NULL.NULL> wrote:
There is no C++ cast to convert between a pointer to function and a
pointer to object (including void *).

reinterpret_cast ?

I haven't used it in a while, but I used to do all sorts of mad stuff
with it back in the day.


According to the Standard, reinterpret_cast can only convert
function pointers to function pointers of different type (converting
function pointer to void* is not listed).

Unfortunately it only lists things you *can* do. Anything not
listed, is not valid. This is what made it difficult for me to find that
particular case I was asking for in my first post.

hth
--
jb

(reply address in rot13, unscramble first)
Feb 15 '06 #7
Jakob Bieling wrote:
Tomás <NU**@NULL.NULL> wrote:
There is no C++ cast to convert between a pointer to function and a
pointer to object (including void *).


reinterpret_cast ?

I haven't used it in a while, but I used to do all sorts of mad stuff
with it back in the day.


According to the Standard, reinterpret_cast can only convert
function pointers to function pointers of different type (converting
function pointer to void* is not listed).

Unfortunately it only lists things you *can* do. Anything not
listed, is not valid. This is what made it difficult for me to find that
particular case I was asking for in my first post.

hth


Nope, you've got to use a C-style cast. Document the hell out of it,
and get managerial approval for such a nasty practice. I've had to do
it that way when trying to print the value of a function pointer -- use
a C-Style cast to cast void (*)(void*) to void* so that I could pass it
to an ostream for display. Pete Becker and I had a thread going about
it here a while back.
Feb 15 '06 #8
red floyd <no*****@here.dude> wrote:
Jakob Bieling wrote:
Tomás <NU**@NULL.NULL> wrote:
There is no C++ cast to convert between a pointer to function and a
pointer to object (including void *). reinterpret_cast ?
According to the Standard, [..] converting
function pointer to void* is not listed [..] Unfortunately it only lists things you *can* do. Anything not
listed, is not valid.

Nope, you've got to use a C-style cast.


Looks like we are agreeing ;) Should have made the point more
obvious.

regards
--
jb

(reply address in rot13, unscramble first)
Feb 15 '06 #9
red floyd posted:
Jakob Bieling wrote:
Tomás <NU**@NULL.NULL> wrote:
There is no C++ cast to convert between a pointer to function and a
pointer to object (including void *).

reinterpret_cast ?

I haven't used it in a while, but I used to do all sorts of mad stuff
with it back in the day.


According to the Standard, reinterpret_cast can only convert
function pointers to function pointers of different type (converting
function pointer to void* is not listed).

Unfortunately it only lists things you *can* do. Anything not
listed, is not valid. This is what made it difficult for me to find
that particular case I was asking for in my first post.

hth


Nope, you've got to use a C-style cast. Document the hell out of it,
and get managerial approval for such a nasty practice. I've had to do
it that way when trying to print the value of a function pointer -- use
a C-Style cast to cast void (*)(void*) to void* so that I could pass it
to an ostream for display. Pete Becker and I had a thread going about
it here a while back.

template<typename T, typename S>
T hardcore_cast(S s)
{
return (T)s;
}

Or something like that.

functionpointer_cast
pointer_cast
-Tomás
Feb 15 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
2
by: Enrique Bustamante | last post by:
Casting arrays that works on watch and command window but not in code. My application is casting arrays in a way it should work. To test if I was doing something invalid, I wrote a test code that...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.