473,387 Members | 3,787 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,387 software developers and data experts.

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 3840
Michael Wagner <mi*********@nc.rr.com> wrote in message
news:mi*******************************@news3-ge0.southeast.rr.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_cast just reinterprets the bit pattern as a
different, possibly completely unrelated, type. You would need
reinterpret_cast 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_cast.

DW

Jul 19 '05 #2

"Michael Wagner" <mi*********@nc.rr.com> wrote in message
news:mi*******************************@news3-ge0.southeast.rr.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*******@mariani.ws> wrote in message
news:bh********@dispatch.concentric.net...
John Harrison wrote:
"Michael Wagner" <mi*********@nc.rr.com> wrote in message
news:mi*******************************@news3-ge0.southeast.rr.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
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...
6
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...
1
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...
6
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: ...
17
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...
20
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...
8
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...
2
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...
0
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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...

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.