Top "Static-cast" questions

A C++ cast operator to convert from one type to another, using only information about the static type of the object being cast

Why use static_cast<int>(x) instead of (int)x?

I've heard that the static_cast function should be preferred to C-style or simple function-style casting. Is this true? Why?

c++ casting static-cast
What is the difference between static_cast<> and C style casting?

Is there any reason to prefer static_cast<> over C style casting? Are they equivalent? Is their any …

c++ casting static-cast
Proper way of casting pointer types

Considering the following code (and the fact that VirtualAlloc() returns a void*): BYTE* pbNext = reinterpret_cast<BYTE*>( VirtualAlloc(…

c++ pointers casting reinterpret-cast static-cast
Should I use static_cast or reinterpret_cast when casting a void* to whatever

Both static_cast and reinterpret_cast seem to work fine for casting void* to another pointer type. Is there a …

c++ pointers static-cast reinterpret-cast
static_cast with boost::shared_ptr?

What is the equivalent of a static_cast with boost::shared_ptr? In other words, how do I have to …

c++ boost shared-ptr static-cast
Why can't I static_cast between char * and unsigned char *?

Apparently the compiler considers them to be unrelated types and hence reinterpret_cast is required. Why is this the rule?

c++ char reinterpret-cast static-cast unsigned-char
C++ cannot convert from base A to derived type B via virtual base A

I have three classes: class A {}; class B : virtual public A {}; class C : virtual public A {}; class D: public B, …

c++ casting downcast virtual-inheritance static-cast
How does qobject_cast work?

I just found the following code in Qt and I'm a bit confused what's happening here. Especially as to what …

c++ qt casting reinterpret-cast static-cast
C++: can't static_cast from double* to int*

When I try to use a static_cast to cast a double* to an int*, I get the following error: …

c++ casting static-cast
Why can't static_cast be used to down-cast when virtual inheritance is involved?

Consider the following code: struct Base {}; struct Derived : public virtual Base {}; void f() { Base* b = new Derived; Derived* d = static_…

c++ virtual-inheritance downcast static-cast