Regular cast vs. static_cast vs. dynamic_cast [duplicate]
Asked Answered
J

8

2043

I've been writing C and C++ code for almost twenty years, but there's one aspect of these languages that I've never really understood. I've obviously used regular casts i.e.

MyClass *m = (MyClass *)ptr;

all over the place, but there seem to be two other types of casts, and I don't know the difference. What's the difference between the following lines of code?

MyClass *m = (MyClass *)ptr;
MyClass *m = static_cast<MyClass *>(ptr);
MyClass *m = dynamic_cast<MyClass *>(ptr);
Joon answered 26/8, 2008 at 13:20 Comment(3)
I would not call the legacy C-style cast a "regular cast" in C++, since it is anything but. You generally shouldn't use in C++, especially with classes, it's just too easy to make mistakes with it. Use of it is a sign of a C programmer who has moved to C++ but hasn't quite learned C++ yet.Resonant
@Vladp In case you're still wondering, or anyone else is reading this and wonders. (Also, for the record, it wasn't a moderator that closed this, it was a user with a dupe-hammer)Pekingese
FYI the linked question has much more upvotes and the answers also have much more upvotes. Also the linked question has some good non-theoretical examples. (Additionally the linked question does not incorrectly refer to C-style typecast syntax as "regular cast".)Seng
E
1849

static_cast

static_cast is used for cases where you basically want to reverse an implicit conversion, with a few restrictions and additions. static_cast performs no runtime checks. This should be used if you know that you refer to an object of a specific type, and thus a check would be unnecessary. Example:

void func(void *data) {
  // Conversion from MyClass* -> void* is implicit
  MyClass *c = static_cast<MyClass*>(data);
  ...
}

int main() {
  MyClass c;
  start_thread(&func, &c)  // func(&c) will be called
      .join();
}

In this example, you know that you passed a MyClass object, and thus there isn't any need for a runtime check to ensure this.

dynamic_cast

dynamic_cast is useful when you don't know what the dynamic type of the object is. It returns a null pointer if the object referred to doesn't contain the type casted to as a base class (when you cast to a reference, a bad_cast exception is thrown in that case).

if (JumpStm *j = dynamic_cast<JumpStm*>(&stm)) {
  ...
} else if (ExprStm *e = dynamic_cast<ExprStm*>(&stm)) {
  ...
}

You can not use dynamic_cast for downcast (casting to a derived class) if the argument type is not polymorphic. For example, the following code is not valid, because Base doesn't contain any virtual function:

struct Base { };
struct Derived : Base { };
int main() {
  Derived d; Base *b = &d;
  dynamic_cast<Derived*>(b); // Invalid
}

An "up-cast" (cast to the base class) is always valid with both static_cast and dynamic_cast, and also without any cast, as an "up-cast" is an implicit conversion (assuming the base class is accessible, i.e. it's a public inheritance).

Regular Cast

These casts are also called C-style cast. A C-style cast is basically identical to trying out a range of sequences of C++ casts, and taking the first C++ cast that works, without ever considering dynamic_cast. Needless to say, this is much more powerful as it combines all of const_cast, static_cast and reinterpret_cast, but it's also unsafe, because it does not use dynamic_cast.

In addition, C-style casts not only allow you to do this, but they also allow you to safely cast to a private base-class, while the "equivalent" static_cast sequence would give you a compile-time error for that.

Some people prefer C-style casts because of their brevity. I use them for numeric casts only, and use the appropriate C++ casts when user defined types are involved, as they provide stricter checking.

Ellingson answered 10/8, 2009 at 13:50 Comment(17)
See also boost's two additional casts: boost.org/doc/libs/1_47_0/libs/conversion/…Angst
@JohannesSchaub-litb: Are you sure that a C style cast lets you 'safely' cast to a private base class? I can see that working when the private base class is the only /base/, but what about virtual/multiple inheritance? I'm assuming the C style cast does no pointer manipulation.Raising
@JohannesSchaub-litb is it true that there is also some overhead involved in using the old c-style casts over the C++ casts?Heavyladen
Are you sure you can write MyClass *c = static_cast<MyClass*>(data); when data is a (void*) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Enabling
@Joseph: Your assumption that "the C style cast does no pointer maniputation" is wrong.Vole
@BenVoigt: Are you sure? I vaguely remember reading that one of the dangers of a C style cast vs. a static_cast was that it would not do pointer arithmetic when dealing with virtual inheritance.Raising
@Joseph: It won't do a cross-cast correctly, or any of the other cases where a runtime check is needed (dynamic_cast is required). But it will do all the same pointer adjustments as static_cast does. Multiple (non-virtual) inheritance is supported just fine, and the correct pointer adjustment will be used.Vole
As @Heavyladen commented, I would like to know as well whether using normal C style cast has some overhead.Candicandia
@JohannesSchaub-litb, you should add something about cross-casting and explain how this happens. I would make a new answer but this is so far upvoted it would be most helpful to add it here.Obara
@Candicandia C style cast does not have the overhead of dynamic cast--it might do a pointer adjustment which is basically just an add or subtract on a pointer. It will invoke built-in (int<->float) and user-defined conversions though, the latter of which can be arbitrarily complex.Obara
Could you explain in more detail why the downcast in the dynamic cast section is invalid? assume Derived had a member m I want to reach, how would this be achieved, if dynamic_cast is not an option?Scarper
@JohannesSchaub-litb, what about time of c-type cast? Is it slower than static_cast?Rikki
can we have a section about the variations we get as pointers to base/derived get offseted by the virtual table pointer ? also these sort of offseting during cast happens with member functions, sometimes a null pointer becomes 1.Musgrove
Can you add some more detail to "In addition, C-style casts not only allow you to do this, but they also allow you to safely cast to a private base-class, while the "equivalent" static_cast sequence would give you a compile-time error for that." ?Outstanding
Another downside of C-style casts is that they are hard to search for in source code.Foot
@JesperJuhl I never needed to search for a specific cast.Ellingson
"It returns a null pointer if the object referred to doesn't contain the type casted to as a base class." Could you explain this sentence a little more? I've read it several times but don't understand it (maybe it's because I'm not a native English speaker). I think your example of dynamic_cast to JumpStm* or ExprStm* is downcasting, but the sentence I'm asking seems to describe upcasting. What am I misunderstanding?Poppas
C
289

Static cast

The static cast performs conversions between compatible types. It is similar to the C-style cast, but is more restrictive. For example, the C-style cast would allow an integer pointer to point to a char.
char c = 10;       // 1 byte
int *p = (int*)&c; // 4 bytes

Since this results in a 4-byte pointer pointing to 1 byte of allocated memory, writing to this pointer will either cause a run-time error or will overwrite some adjacent memory.

*p = 5; // run-time error: stack corruption

In contrast to the C-style cast, the static cast will allow the compiler to check that the pointer and pointee data types are compatible, which allows the programmer to catch this incorrect pointer assignment during compilation.

int *q = static_cast<int*>(&c); // compile-time error

Reinterpret cast

To force the pointer conversion, in the same way as the C-style cast does in the background, the reinterpret cast would be used instead.

int *r = reinterpret_cast<int*>(&c); // forced conversion

This cast handles conversions between certain unrelated types, such as from one pointer type to another incompatible pointer type. It will simply perform a binary copy of the data without altering the underlying bit pattern. Note that the result of such a low-level operation is system-specific and therefore not portable. It should be used with caution if it cannot be avoided altogether.

Dynamic cast

This one is only used to convert object pointers and object references into other pointer or reference types in the inheritance hierarchy. It is the only cast that makes sure that the object pointed to can be converted, by performing a run-time check that the pointer refers to a complete object of the destination type. For this run-time check to be possible the object must be polymorphic. That is, the class must define or inherit at least one virtual function. This is because the compiler will only generate the needed run-time type information for such objects.

Dynamic cast examples

In the example below, a MyChild pointer is converted into a MyBase pointer using a dynamic cast. This derived-to-base conversion succeeds, because the Child object includes a complete Base object.

class MyBase 
{ 
  public:
  virtual void test() {}
};
class MyChild : public MyBase {};



int main()
{
  MyChild *child = new MyChild();
  MyBase  *base = dynamic_cast<MyBase*>(child); // ok
}

The next example attempts to convert a MyBase pointer to a MyChild pointer. Since the Base object does not contain a complete Child object this pointer conversion will fail. To indicate this, the dynamic cast returns a null pointer. This gives a convenient way to check whether or not a conversion has succeeded during run-time.

MyBase  *base = new MyBase();
MyChild *child = dynamic_cast<MyChild*>(base);

 
if (child == 0) 
std::cout << "Null pointer returned";

If a reference is converted instead of a pointer, the dynamic cast will then fail by throwing a bad_cast exception. This needs to be handled using a try-catch statement.

#include <exception>
// …  
try
{ 
  MyChild &child = dynamic_cast<MyChild&>(*base);
}
catch(std::bad_cast &e) 
{ 
  std::cout << e.what(); // bad dynamic_cast
}

Dynamic or static cast

The advantage of using a dynamic cast is that it allows the programmer to check whether or not a conversion has succeeded during run-time. The disadvantage is that there is a performance overhead associated with doing this check. For this reason using a static cast would have been preferable in the first example, because a derived-to-base conversion will never fail.

MyBase *base = static_cast<MyBase*>(child); // ok

However, in the second example the conversion may lead to run-time error. The error will happen if the MyBase object contains a MyBase instance but not if it contains a MyChild instance. In some situations this may not be known until run-time. When this is the case dynamic cast is a better choice than static cast.

// Succeeds for a MyChild object
MyChild *child = dynamic_cast<MyChild*>(base);

If the base-to-derived conversion had been performed using a static cast instead of a dynamic cast the conversion would not have failed. It would have returned a pointer that referred to an incomplete object. Dereferencing such a pointer can lead to run-time errors.

// Allowed, but invalid
MyChild *child = static_cast<MyChild*>(base);
 
// Incomplete MyChild object dereferenced
(*child);

Const cast

This one is primarily used to add or remove the const modifier of a variable.

const int myConst = 5;
int *nonConst = const_cast<int*>(&myConst); // removes const

Although const cast allows the value of a constant to be changed, doing so is still invalid code that may cause a run-time error. This could occur for example if the constant was located in a section of read-only memory.

*nonConst = 10; // potential run-time error

const cast is instead used mainly when there is a function that takes a non-constant pointer argument, even though it does not modify the pointee.

void print(int *p) 
{
   std::cout << *p;
}

The function can then be passed a constant variable by using a const cast.

print(&myConst); // error: cannot convert 
                 // const int* to int*
 
print(nonConst); // allowed

Source and More Explanations

Custer answered 26/8, 2008 at 13:21 Comment(6)
std::bad_cast is defined in <typeinfo>Surge
from child to base, cast is not necessary: MyBase *base = child; // okPeirce
In my opinion, the best answer, very simple and yet clearShagreen
this should really be the top answer imoCosper
This is the best answer.Euphony
I ran this line , "*p = 5; // run-time error: stack corruption ", on both GCC and Visual Studio, does not give me runtime error. !!??Denotative
I
92

You should look at the article C++ Programming/Type Casting.

It contains a good description of all of the different cast types. The following taken from the above link:

const_cast

const_cast(expression) The const_cast<>() is used to add/remove const(ness) (or volatile-ness) of a variable.

static_cast

static_cast(expression) The static_cast<>() is used to cast between the integer types. 'e.g.' char->long, int->short etc.

Static cast is also used to cast pointers to related types, for example casting void* to the appropriate type.

dynamic_cast

Dynamic cast is used to convert pointers and references at run-time, generally for the purpose of casting a pointer or reference up or down an inheritance chain (inheritance hierarchy).

dynamic_cast(expression)

The target type must be a pointer or reference type, and the expression must evaluate to a pointer or reference. Dynamic cast works only when the type of object to which the expression refers is compatible with the target type and the base class has at least one virtual member function. If not, and the type of expression being cast is a pointer, NULL is returned, if a dynamic cast on a reference fails, a bad_cast exception is thrown. When it doesn't fail, dynamic cast returns a pointer or reference of the target type to the object to which expression referred.

reinterpret_cast

Reinterpret cast simply casts one type bitwise to another. Any pointer or integral type can be casted to any other with reinterpret cast, easily allowing for misuse. For instance, with reinterpret cast one might, unsafely, cast an integer pointer to a string pointer.

Invocation answered 26/8, 2008 at 13:28 Comment(0)
C
34

FYI, I believe Bjarne Stroustrup is quoted as saying that C-style casts are to be avoided and that you should use static_cast or dynamic_cast if at all possible.

Barne Stroustrup's C++ style FAQ

Take that advice for what you will. I'm far from being a C++ guru.

Cherriecherrita answered 26/8, 2008 at 13:39 Comment(3)
^ Yeah, because C++ casts that are explicitly labelled and deliberately confined to well-defined roles are more "hellish" than a C cast, which just blindly tries multiple types of cast until anything works, regardless of sense... good one.Tensor
Actually, if you read his FAQ, Stroustrup recommends you avoid casts all together. The first sentence in his section on static_cast: "Casts are generally best avoided."Reinhardt
@BillWeinman in practice you cannot avoid casts altogether (and as far as I'm concerned, the wording "best avoided" allows for that). As soon as you interface your own code with APIs or different APIs to each other, more often than not you are presented with situations where the types don't match up exactly and you will have to resort to casting. This is especially true for older and organically grown APIs. WinAPI being a prime example.Tiler
I
27

Avoid using C-Style casts.

C-style casts are a mix of const and reinterpret cast, and it's difficult to find-and-replace in your code. A C++ application programmer should avoid C-style cast.

Instill answered 19/9, 2008 at 17:30 Comment(0)
U
15

C-style casts conflate const_cast, static_cast, and reinterpret_cast.

I wish C++ didn't have C-style casts. C++ casts stand out properly (as they should; casts are normally indicative of doing something bad) and properly distinguish between the different kinds of conversion that casts perform. They also permit similar-looking functions to be written, e.g. boost::lexical_cast, which is quite nice from a consistency perspective.

Ucayali answered 26/8, 2008 at 13:38 Comment(0)
G
14

dynamic_cast has runtime type checking and only works with references and pointers, whereas static_cast does not offer runtime type checking. For complete information, see the MSDN article static_cast Operator.

Gooding answered 26/8, 2008 at 13:26 Comment(0)
H
13

dynamic_cast only supports pointer and reference types. It returns NULL if the cast is impossible if the type is a pointer or throws an exception if the type is a reference type. Hence, dynamic_cast can be used to check if an object is of a given type, static_cast cannot (you will simply end up with an invalid value).

C-style (and other) casts have been covered in the other answers.

Housewife answered 5/2, 2012 at 17:10 Comment(1)
"you will simply end up with an invalid value" and an undefined behavior. That is, the program was misbehave even if you don't use the valueDecern

© 2022 - 2024 — McMap. All rights reserved.