0

Possible Duplicate:
Why do we have reinterpret_cast in C++ when two chained static_cast can do it's job?

I have been suggested that i should not use reinterpret_cast or const_cast in case of pointer to pointer conversion. Only dynamic_cast should be used.Because other cast can create problem in future. So my question is why not reinterpret_cast or other cast which is dangerous has been removed from c++ standard.

Community
  • 1
  • 1
Ganesh
  • 71
  • 4
  • 5
  • 1
    Duplicate of [Why do we have reinterpret_cast in C++ when two chained static_cast can do it's job?](http://stackoverflow.com/questions/5025843/why-do-we-have-reinterpret-cast-in-c-when-two-chained-static-cast-can-do-its-j). The basic answer is: because there are things that cannot be done with the other cast expressions. – James McNellis Mar 04 '11 at 04:39
  • See this topic : [Why do we have reinterpret_cast in C++ when two chained static_cast can do it's job?](http://stackoverflow.com/questions/5025843/why-do-we-have-reinterpret-cast-in-c-when-two-chained-static-cast-can-do-its-j) – Nawaz Mar 04 '11 at 04:40

5 Answers5

6

Because there are times when you need a cast which does what reinterpret_cast does. As for dynamic_cast, I almost never use this cast; that's only for casting from a parent type to a more derived type. Most of the time I can prove which child type I'm working with, and I use static_cast. I also use static_cast for compile time type conversions, for example from signed to unsigned integers.

static_cast, not dynamic_cast, is the most common kind of cast. If your design relies on dynamic_cast too heavily, that's a code smell indicating your class hierarchy violates LSP in most cases.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
3

In the real world, you frequently have to cast pointers in ways the compiler/runtime can't validate. For example, in pthreads you pass a void* to the new thread's start routine. Sometimes that arg is actually a class? The compiler has no way to tell. It's one of those "real life" issues.

Incidentally I find myself using dynamic_cast infrequently. My main use for is has been exception type scraping in catch blocks.

seand
  • 5,168
  • 1
  • 24
  • 37
2

They are dangerous but sometimes you need them.

C++ is not know for removing constructs that are dangerous to the new user. We will let you run with those scissors (while eating cake).

What makes them good is that the dangerous code sticks out so it is easy to spot. So when people do code reviews they can quickly spot the stuff that is dangerous and add a little scrutiny more checking.

Martin York
  • 257,169
  • 86
  • 333
  • 562
  • +1 for mentioning how it makes the code stick out. good reason to use C++ vs. C style casts – seand Mar 04 '11 at 04:44
1

Use reinterpret_cast for casting between unrelated pointer types.

Use static_cast for explicit, supported conversions.

Use dynamic_cast to cast a pointer of one type to a pointer of a derived type.

If you know a pointer to a parent type points to a child type, you can safely static_cast from the parent type to the child type. A cast from a child type pointer to a parent type pointer is implicit and requires no explicit cast.

A reinterpret_cast example from my own code base:

unsigned int CTaskManager::CWorker::WorkerMain(void* Parameters)
{
    CWorker* This = reinterpret_cast<CWorker*>(Parameters);
    // ...
    }

bool CTaskManager::CWorker::Initialize()
{
    // ...

    // Create worker.
    m_ThreadHandle = reinterpret_cast<HANDLE>(_beginthreadex(NULL, 0, &(WorkerMain), this, 0, NULL));

    // ...

}
Sion Sheevok
  • 4,057
  • 2
  • 21
  • 37
0

A lot of dangerous operations, though they should generally be avoided, do have a rare legitimate usage. Furthermore, the rule of thumb when it comes to language features and APIs is that once it's there, you can't get rid of it; removing a feature from the C++ language has the potential to break lots of existing C++ code. Typically removal of features requires demonstration that it is not used or that the use is so limited that the cost impact of getting rid of it would be small. Even trigraphs, which are almost never used (unless you are at IBM) and which people wanted to get rid of survived the axe. These various casts and other usually dangerous operations are used way, way more than trigraphs are.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200