473,549 Members | 2,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When to use various types of casts?


I do some Windows kernel programming, where what I need to pass to some
Kernel call is "void* Context". Sometime later, I will get that Conext
back. I want to pass a class pointer to this system class, and then pass
the void* back to the class when the kernel calls be back.

I am not clear which of the casts I really want to use in this case,
though I am pretty sure that I don't want dynamic casts or const casts.
Do I want static or reinterpret casts in this case?

Thanks,

Mike
Jul 19 '05 #1
4 3854
Michael Wagner <mi*********@nc .rr.com> wrote in message
news:mi******** *************** ********@news3-ge0.southeast.r r.com...

I do some Windows kernel programming, where what I need to pass to some
Kernel call is "void* Context". Sometime later, I will get that Conext
back. I want to pass a class pointer to this system class, and then pass
the void* back to the class when the kernel calls be back.

I am not clear which of the casts I really want to use in this case,
though I am pretty sure that I don't want dynamic casts or const casts.
Do I want static or reinterpret casts in this case?


For converting a void * to some type of data pointer, use static_cast.
static_cast does more checking, and will actually do a conversion if
necessary, whereas reinterpret_cas t just reinterprets the bit pattern as a
different, possibly completely unrelated, type. You would need
reinterpret_cas t to convert an int * to a float *, for example, because
it's a conversion that makes no sense.

static_cast is therefore safer than reinterpret cast (but not necessarily
actually safe). Unless you know for certain that reinterpreting a bit
pattern rather than a conversion is what you want, then use static_cast. If
the compiler complains that it cannot do a static_cast (and you are still
quite sure that you want to go ahead with the cast) then use
reinterpret_cas t.

DW

Jul 19 '05 #2

"Michael Wagner" <mi*********@nc .rr.com> wrote in message
news:mi******** *************** ********@news3-ge0.southeast.r r.com...

I do some Windows kernel programming, where what I need to pass to some
Kernel call is "void* Context". Sometime later, I will get that Conext
back. I want to pass a class pointer to this system class, and then pass
the void* back to the class when the kernel calls be back.

I am not clear which of the casts I really want to use in this case,
though I am pretty sure that I don't want dynamic casts or const casts.
Do I want static or reinterpret casts in this case?

Thanks,

Mike


Use static_cast for 'related' types, float to int for instance (since
they're both numbers). All pointers are related to void*, so your case is a
static_cast.

john
Jul 19 '05 #3

"Gianni Mariani" <gi*******@mari ani.ws> wrote in message
news:bh******** @dispatch.conce ntric.net...
John Harrison wrote:
"Michael Wagner" <mi*********@nc .rr.com> wrote in message
news:mi******** *************** ********@news3-ge0.southeast.r r.com...
I do some Windows kernel programming, where what I need to pass to some
Kernel call is "void* Context". Sometime later, I will get that Conext
back. I want to pass a class pointer to this system class, and then pass
the void* back to the class when the kernel calls be back.

I am not clear which of the casts I really want to use in this case,
though I am pretty sure that I don't want dynamic casts or const casts.
Do I want static or reinterpret casts in this case?

Thanks,

Mike

Use static_cast for 'related' types, float to int for instance (since
they're both numbers). All pointers are related to void*, so your case is a static_cast.

All pointers are not necessarily related.

struct A {}; struct B {};

B * pb = 0;

A * pa = static_cast<A *>( pb );
// error: invalid static_cast from type `B*' to type `A*'

A * ypa = static_cast<A *>( (void *) 0 ); // legal


I didn't say all pointers are related, only all pointers are related to
void*. This does mean you could use two static_casts to get from any pointer
type to any other pointer type, but I don't think I'd recommend that.

I should have said all data pointers are related to void*. Function pointers
and member pointers are something else again.

john
Jul 19 '05 #4
Gianni Mariani wrote:
Use static_cast for 'related' types, float to int for instance (since
they're both numbers). All pointers are related to void*, so your ^^^^^^^^ case is a static_cast.

All pointers are not necessarily related.


That's not what John wrote.
Jul 19 '05 #5

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

Similar topics

1
2057
by: J. Campbell | last post by:
I have a feeling that I'm doing things all ass-backwards (again ;-), and would like some advice. What I want to do is: put some data to memory and then access that memory space as an array of data-types of my choosing (eg an array of char, short, or int). The application has to do with generating checksum-type values for files or strings,...
6
1571
by: Eric Lilja | last post by:
Hello, I have a std::vector storing pointers to an abstract base class OptionBase. OptionBase is not templated but I am deriving a template class called Option from OptionBase. The class Option has a public member function called get_value() that is not present in the class OptionBase. My problem is that when calling get_value() I have to cast...
1
1907
by: PengYu.UT | last post by:
Hi, An example usage of static_cast is at http://publib.boulder.ibm.com/infocenter/macxhelp/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc05keyword_static_cast.htm But I don't understand why it is necessary to use float d = static_cast<float>(j)/v; Isn't float d = float(j)/v;
6
2004
by: Jean-Christian Imbeault | last post by:
Is it right for postgres to accept a foreign key constraint when the type of the field is not the same as that of the foreign key? For example: # Create table a (id int primary key); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'a_pkey' for table 'a' CREATE TABLE # Create table b (id2 text references a(id));
17
3129
by: Chris Travers | last post by:
Hi all; I just made an interesting discovery. Not sure if it is a good thing or not, and using it certainly breakes first normal form.... Not even sure if it really works. However, as I am able to CRASH the backend, there is a bug here somewhere... test=# select version(); version
20
5322
by: Hanzac Chen | last post by:
Hi, I don't understand why this could happen? The Code 1 will output `fff9' and the Code 2 will output `1' How could the `mod 8' not have effect? /* Code 1 */ #include <stdio.h> #include <stdlib.h>
8
2170
by: Alex Vinokur | last post by:
Here is a code from http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8 -------------------------------------- class Shape { public: virtual ~Shape() { } // A virtual destructor virtual void draw() = 0; // A pure virtual function virtual void move() = 0; ...
2
2785
by: Jim S | last post by:
I have a .net framework 2.0 client (Pocket PC) and a .net 2.0 webservice that communicate on the same LAN. The Pocket PC has no problem consuming strings returned from the web service methoeds but I’m unable to receive and a custom object. The error occurs when the PocketPC app casts a returned object (WebServiceServer.objectA) from the...
0
1172
by: jehugaleahsa | last post by:
Try this instead: // T - the type you are attempting to retrieve. // o - the data that you want to turn into a T T get<T>(object o) { // handle nulls - turns Nullables into null, reference types to null and value types to 0. if (o == null || o == DBNull.Value) {
0
7467
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7736
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7827
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5385
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5110
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3494
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1961
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1079
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.