473,851 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ style casting

I've always been a little sketchy on the differences between static,
dynamic, and reinterpret casting. I am looking to clean up the
following block by using C++ casting instead of the C style casting.
from what I am reading, I should use reinterpret cast in this
situation, is that correct? Why does static and dynamic casting fail
me?

// please excuse the windows types, it is necessary in this code,
// but the question remains C++ related

BYTE * textureBuffer = static_cast<BYT E *>(data.pBits) ;
DWORD colorkey = *((LPDWORD)((DW ORD)textureBuff er));

for(unsigned y = 0; y < info.Height; y++)
{
for(unsigned x = 0; x < info.Width; x++)
{
// Get the pixel at the current row and column
// with confidence that there are 4 bytes per pixel
// because we guarenteed earlier that the format
// is A8RG8B8
DWORD * pixel = ((LPDWORD)((DWO RD)textureBuffe r + y *
data.Pitch + x * 4));
fout << "\t" << std::hex << (*pixel);
}
fout << std::endl;
}

fout.close();

Nov 30 '06 #1
5 5936

brekehan wrote:
I've always been a little sketchy on the differences between static,
dynamic, and reinterpret casting.
static_cast

static_cast is used to perform a cast between related types. Any of the
basic types (char, double, int, etc) can be converted using this cast.
It can also be used to up-cast or downcast classes in the same
inheritance tree (however, review dynamic_cast for reasons why you
might not want to do this). It correctly performs any necessary pointer
movement in order to point at the correct location within the object
for that static type. Consider that static almost always means
something performed at compile time that results in a hard coded
manipulation of the object into a new type.
dynamic_cast

dynamic_cast will perform a run-time cast of a polymorphic type. This
means that it is a safer but slower cast to objects sharing an
inheritance tree. This is usually used for a down cast that checks for
the validity of the cast before continuing. When a dynamic cast is
performed the system checks whether the object of cast is actually of
the type being cast to. Review the following:

class A {}; class B : public A {}; class C : public A {};

....

A * b = new B; A * c = new C;

....

C * bad = static_cast<C>( b); // also the result of a c style cast.

In this case the compiler happily performs the necessary conversion
between the related types B* and C* but the cast is obviously not
valid. Later something could blow up because B and C don't have the
same alignment properties. See the following:

C * bad = dynamic_cast<C> (b); If (bad)...ok.

In this case a dynamic, or run-time cast was used. The result of this
cast is a null pointer because b is not a C* and the cast is not valid.
This can now be checked and validated so that the program doesn't
just crash because someone didn't account for some situation when a
B* could be passed into a function that did this cast.

When this cast is performed on a reference it results in a bad_cast
exception instead of a null pointer.

const_cast

This is rarely necessary. If you need a const cast you should review
why and take the problem to the program manager for review. Do not cast
away constness without this review process.

Const cast is used to cast away constness or volatility. We do not use
volatility currently in pipe-flo so I will focus on constness. Const
cast should NEVER be used unless you are casting TO const. The
following is an example:

const char * x = "hello"; char * y = const_cast<char *>(x);

The goal of the above code is probably to change the text pointed to by
x. This results in undefined behavior and must not be done.

Sometimes one might be tempted to cast away constness in order to call
a non-const function that you are sure won't change anything. The
correct way to fix this problem is to make the function const. There
are a few cases when it is appropriate to change values internal to a
class in a const function (this also must be reviewed by the program
manager) and you can do this by making that variable "mutable".

One situation that const_cast is allowed without review is casting into
const:

class Obj {

...

void f() const { do stuff without changing anything}; void f() { do
something that changes stuff; const_cast<cons t Obj*>(this)->f(); } };

That uses the const version of f() instead of rewriting the same code.
This is of questionable design but not invalid so is allowed.

reinterpret_cas t

This cast is used to cast between totally unrelated types. For
instance, you may have a C function that accepts char* as input and you
want to pass in an array of integers instead (often seen in binary file
output). This would be done as follows:

void cf(const char * input, size_t size);

.... int x[] = {5, 11, 23 }; cf(reinterpret_ cast<char*>(x), sizeof(int)
* 3);

Notice that a const cast is not necessary because you can always add
const without a cast.

The above code formally results in undefined behavior but is necessary
when dealing with C code that doesn't have the strong typing and
templates available in C++.

You can always perform a cast from one type to another and back
(assuming that the size of the destination type is large enough to hold
the entire value of the source) and this will result in defined
behavior. For instance, passing data into a C callback (any win32 dlg
proc) you will often want to cast some pointer to the LPARAM type:

reinterpret_cas t<LPARAM>(an_in teger)

Later you can cast back:

reinterpret_cas t<int>(lParam) ;

This is valid and defined code since the LPARAM type is large enough to
store any int value.

Nov 30 '06 #2
brekehan wrote:
I've always been a little sketchy on the differences between static,
dynamic, and reinterpret casting. I am looking to clean up the
following block by using C++ casting instead of the C style casting.
from what I am reading, I should use reinterpret cast in this
situation, is that correct? Why does static and dynamic casting fail
me?

// please excuse the windows types, it is necessary in this code,
// but the question remains C++ related

BYTE * textureBuffer = static_cast<BYT E *>(data.pBits) ;
DWORD colorkey = *((LPDWORD)((DW ORD)textureBuff er));
* reinterpret_cas t<LPDWORD>(
reinterpret_cas t<char *>(textureBuffe r)
)
....
>
for(unsigned y = 0; y < info.Height; y++)
{
for(unsigned x = 0; x < info.Width; x++)
{
// Get the pixel at the current row and column
// with confidence that there are 4 bytes per pixel
// because we guarenteed earlier that the format
// is A8RG8B8
DWORD * pixel = ((LPDWORD)((DWO RD)textureBuffe r + y *
data.Pitch + x * 4));
fout << "\t" << std::hex << (*pixel);
}
fout << std::endl;
}

fout.close();
Nov 30 '06 #3
Noah Roberts:
static_cast

static_cast is used to perform a cast between related types. Any of the
basic types (char, double, int, etc) can be converted using this cast.
It can also be used to up-cast or downcast classes in the same
inheritance tree (however, review dynamic_cast for reasons why you
might not want to do this). It correctly performs any necessary pointer
movement in order to point at the correct location within the object
for that static type. Consider that static almost always means
something performed at compile time that results in a hard coded
manipulation of the object into a new type.

I think an exhaustive list of uses for static_cast would be:

(1) Implicit conversions with supression of compiler warnings.
(2) Casting from void* to another pointer type.
(3) Casting from Base pointers and references to Derived.

const_cast

This is rarely necessary.

const_cast can be handly for avoiding code duplication. You might have seen
my recent post which contained the macro NON_CONST_IN_TE RMS_OF.

reinterpret_cas t

This cast is used to cast between totally unrelated types. For
instance, you may have a C function that accepts char* as input and you
want to pass in an array of integers instead (often seen in binary file
output). This would be done as follows:

void cf(const char * input, size_t size);

... int x[] = {5, 11, 23 }; cf(reinterpret_ cast<char*>(x), sizeof(int)
* 3);

Notice that a const cast is not necessary because you can always add
const without a cast.

The above code formally results in undefined behavior but is necessary
when dealing with C code that doesn't have the strong typing and
templates available in C++.

The behaviour is well-defined, as any address can be realiably stored in a
char* (because objects are nothing more than a finite sequence of bytes in
memory). Even before the advent of C++, proficient programmers had been
doing the following for decades:

unsigned arr[64];

char *p = (char*)arr;

There is a common misconception that reinterpret_cas t is nothing but a
cowboy and a wild animal. There a several legitimate and well-defined uses
for reinterpret_cas t.

You can always perform a cast from one type to another and back
(assuming that the size of the destination type is large enough to hold
the entire value of the source) and this will result in defined
behavior.

This only applies for "pointer type" to "integer type". I wouldn't try
store a pointer value in a double, regardless of how many bits a double
has...

--

Frederick Gotham
Dec 1 '06 #4
brekehan:
I've always been a little sketchy on the differences between static,
dynamic, and reinterpret casting. I am looking to clean up the
following block by using C++ casting instead of the C style casting.
from what I am reading, I should use reinterpret cast in this
situation, is that correct? Why does static and dynamic casting fail
me?

If you want to replace all "(Type)" casts with the "xxxx_cast" family, then
the following is a quick reliable method which doesn't require any
intelligence. (Before I begin, I'll assume that the code you're dealing
with has no need for dynamic_cast.)

Replace all casts with "static_cas t", then re-compile. If you don't get any
errors, then you're good to go. If you _do_ get errors though, go through
the error lines and replace the offending static_cast's with const_cast.
Re-compile again. If you get errors for the const_cast's, then replace the
offending const_cast's with reinterpret_cas t. If you still get errors for
the reinterpret_cas t's, then you'll need a combination of reinterpret_cas t
and const_cast.

The following is an example where a "(Type)" style cast would need to turn
into two casts:

int const arr[4] = {0,1,2,3};

char *p = (char*)arr;

would become:

char *p = reinterpret_cas t<char*>( const_cast<int( &)[4]>(arr) );

or, the more convenient method which doesn't need to know the type of
"arr":

char *p = const_cast<char *>(
reinterpret_cas t<char const volatile*>(arr) );

Lately, I've been intentionally paranoid about new features being added to
the language which could possibly break code like this, so I think it might
be wise to replace "char const volatile" with something like:

typedef char const volatile most_strict_cha r;

That way, if a new modifier like "cache" is added to the language, we just
need to add it to the typedef:

typedef char const volatile cache most_strict_cha r;

Then again, we could always make a function out of it, or even a macro:

#define MOST_STRICT_TYP E(type) type const volatile

--

Frederick Gotham
Dec 1 '06 #5
Frederick Gotham:
#define MOST_STRICT_TYP E(type) type const volatile
Or the handier solution:

template<class T>
struct MostStrictType {
typedef T const volatile Type;
};

--

Frederick Gotham
Dec 1 '06 #6

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

Similar topics

26
2571
by: Steven T. Hatton | last post by:
The code shown below is an example from the Coin3D documentation. I believe the use of the C-style cast is safe under the circumstances, but from what I've been exposed to (TC++PL(SE)), I would favor using a static_cast. Is there any technical reason to favor the C-style over a static_cast? http://doc.coin3d.org/Coin/index.html void foo(SoNode * node) { if (node->getTypeId() == SoFile::getClassTypeId()) {
15
1742
by: Christopher Benson-Manica | last post by:
If you had an unsigned int that needed to be cast to a const myClass*, would you use const myClass* a=reinterpret_cast<const myClass*>(my_val); or const myClass* a=(const myClass*)myVal; ?
3
1477
by: Howard | last post by:
Hi, I am maintaining a lot of code that is rife with C-style casts. I've seen a lot of comments that one should not use C-style casts at all. But I'm wondering what harm there could be in doing so with the built-in types. For example, if you have a long which needs to be passed to a function as an unsigned long, or a char* that needs to be passed as an unsigned char*, or an unsigned int that needs to be passed as a long, isn't it...
15
2009
by: Earl Higgins | last post by:
The company where I work as a Senior Software Engineer is currently revamping their (1991 era) "Programming and Style Guidelines", and I'm on the committee. The company, in business for over 20 years, writes medical software, which must ultimately pass FDA approval and ISO 9000 certification. The product the document applies to is a large system, (just over 3 million lines of code) running on a variety of platforms (all Unix-ish), and over...
19
1759
by: Ramesh Tharma | last post by:
Hi, Is any one knows what's wrong with the following code, I was told that it will compile and run but it will crash for some values. Assume that variables are initilized. char* c; long* lg;
5
10233
by: Chuck Bowling | last post by:
Maybe I'm doing something wrong or just don't understand the concept, but i'm having a problem with default properties. My impression of how a default property should act is this; MyClass c = new MyClass(); c = "my text"; In the line above, the string is assigned to a field in the class instance.
4
1890
by: 0to60 | last post by:
I'm coding in MC++ and I'm using the System::Collections data structures to store my own objects. When I get something out of a hashmap, should I be using dynamic_cast or old C-style casting? In other words, should it be Namespace::ObjectName* someObject = (Namespace::ObjectName*)hashMap->Item(key); or Namespace::ObjectName* someObject =
3
6005
by: Kobe | last post by:
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>
2
1386
by: Noah Roberts | last post by:
I have a class, that inherits from a class that inherits virtually from another class. I have a breakdown occuring and it is not wrt the virtually inherited class but one of the other MIed pure virtual classes. I get this in my call stack at the point of explosion: Flo.exe!DDocument::Dispatch(DFZone * ptr=0x0298d260) Line 94 + 0x32 bytes C++ Flo.exe!DDocument::Dispatch(DFZone * __A0=0x0298d260) + 0x2f
0
9898
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10670
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10735
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10356
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7907
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7073
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5736
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5934
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3180
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.