Hi,
if I need to convert a size_t to an int, in "older" C++ I'd write the
following code (using C-like "casting"):
<CODE>
std::vector<...> v;
int count = (int) v.size();
// v.size() returns size_t
</CODE>
Now that modern C++ has different cast tools, how the above code should
be changed? Should I use static_cast or reinterpret_cast ?
e.g.
int count = static_cast<int>(v.size());
Moreover, sometimes I need to convert 32 bits unsigned ints (typedef
unsigned int DWORD) into pointers.
e.g.
<CODE>
// Old style C casting
DWORD address;
// address = ...
AClass * myObject = (AClass *)address;
</CODE>
Should I use static_cast or reinterpret_cast in this case?
Thanks in advance,
K 3 5966
Kobe wrote: if I need to convert a size_t to an int, in "older" C++ I'd write the following code (using C-like "casting"):
<CODE> std::vector<...> v; int count = (int) v.size(); // v.size() returns size_t </CODE>
Now that modern C++ has different cast tools, how the above code should be changed? Should I use static_cast or reinterpret_cast ?
e.g.
int count = static_cast<int>(v.size());
That should suffice. Beware, though, that if the value of 'v.size()'
cannot be represented in an int, the behaviour is implementation-
defined, and you may end up with a negative 'count'. Do you really need
'count' to be 'int'?
Moreover, sometimes I need to convert 32 bits unsigned ints (typedef unsigned int DWORD) into pointers.
Are you sure they will fit?
e.g.
<CODE> // Old style C casting DWORD address; // address = ... AClass * myObject = (AClass *)address; </CODE>
Should I use static_cast or reinterpret_cast in this case?
Only 'reinterpret_cast' will do.
V
--
Please remove capital As from my address when replying by mail
Kobe wrote: Hi,
if I need to convert a size_t to an int, in "older" C++ I'd write the following code (using C-like "casting"):
<CODE> std::vector<...> v; int count = (int) v.size(); // v.size() returns size_t </CODE>
Now that modern C++ has different cast tools, how the above code should be changed? Should I use static_cast or reinterpret_cast ?
e.g.
int count = static_cast<int>(v.size());
That should work with the obvious limitation.
What's wring with:
typedef std::vector<...> myContainer;
myContainer v;
myContainer::size_type count = v.size();
Moreover, sometimes I need to convert 32 bits unsigned ints (typedef unsigned int DWORD) into pointers.
<shudder/>
<CODE> // Old style C casting DWORD address; // address = ... AClass * myObject = (AClass *)address; </CODE>
Should I use static_cast or reinterpret_cast in this case?
reinterpret_cast is the tool for the job.
Preferably, don't, of course.
Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Kobe posted: Hi,
if I need to convert a size_t to an int, in "older" C++ I'd write the following code (using C-like "casting"):
<CODE> std::vector<...> v; int count = (int) v.size(); // v.size() returns size_t </CODE>
Now that modern C++ has different cast tools, how the above code should be changed? Should I use static_cast or reinterpret_cast ?
e.g.
int count = static_cast<int>(v.size());
Moreover, sometimes I need to convert 32 bits unsigned ints (typedef unsigned int DWORD) into pointers.
e.g.
<CODE> // Old style C casting DWORD address; // address = ... AClass * myObject = (AClass *)address; </CODE>
Should I use static_cast or reinterpret_cast in this case?
Thanks in advance, K
Here's what I'd do:
Where ever you have a C-style cast, replace it with "static_cast".
Then recompile it.
If "static_cast" causes any errors, then change the ones that cause
errors to "reinterpret_cast".
Basically, "reinterpret_cast" can do everything thing that "static_cast"
does, and more.
-Tomás This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Marc Schellens |
last post by:
Can somebody tell me when exactly a static_cast is invalid,
and when not?
thanks,
marc
|
by: Suzanne Vogel |
last post by:
I'd like to convert a double to a binary representation. I can use the
"&" bit operation with a bit mask to convert *non* float types to binary
representations, but I can't use "&" on doubles.
...
|
by: Scott Brady Drummonds |
last post by:
Hi, everyone,
I've checked a couple of on-line resources and am unable to determine how
reinterpret_cast<> is different from static_cast<>. They both seem to
perform a compile-time casting of...
|
by: Amit |
last post by:
Greetings.
I am having some problem while using a cast operation(static_cast and/or
dynamic_cast) between base and derived objects when passing to functions.
what I have is something like this..
...
|
by: Rahul |
last post by:
Hi,
I have a
class A : public B {...member functions......data members};
and am doing the following
A *p=new A();
void *p=static_cast<void *>(p);
factory_instance->process(p);
|
by: richard_lavoie |
last post by:
Hi,
I have something like this:
vector<floatvec1;
and I want to cast it, so I use
vector vec2<double= static_cast< vector<double(vec1);
I always become a
error: syntax error before `>'...
|
by: jasm |
last post by:
hello everybody!
I have used reinterpret_cast for interpret a class object as a char*.
This is the object:
template<class T>
class Coordinates {
public:
T *x;
T *y;
|
by: =?utf-8?B?5Zyw55CD5Y+R5Yqo5py6?= |
last post by:
In C++ we can not overload the explicit typecast operator like
static_cast, dynamic_cast, const_cast and reinterpret_cast. The only
we can do is the implicit typecast operator like operator...
|
by: ciccio |
last post by:
Hi,
I was wondering what the main reason is why reinterpret_cast fails to work
as expected when using optimizations. Here is the simple example code which
fail to give the correct result when...
|
by: zeeshan708 |
last post by:
what is the difference between a reinterpret_cast and static_cast,,both do casting at compile time so whats the difference????
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |