5

I have been reading some other threads on this site, and they mention how dynamic_cast and static_cast are both safe for upcasting.

Why are these even required for upcasting?

For example, if class B is derived from A, then

A * ptr = new B ();

still works, and behaves like an object of type A. (I am also from a Java background where conversion for upcasting is unnecessary.

I also read on this site that dynamic_cast is not needed for downcasting [in question "When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?"] . Again, I would think that casting is only really necessary when you are downcasting since upcasting occurs automatically.

Where am I wrong?

Community
  • 1
  • 1
user929404
  • 2,153
  • 1
  • 22
  • 27
  • Note: in the prior question, one of the answers says "You don't have to use it to cast downwards ...". However, from the context of the full sentence, the meaning is that you're not limited to using it only for casting downwards. [I've since edited that answer to remove the ambiguity.] – Andy Thomas Apr 29 '13 at 18:58

3 Answers3

3

If you have complex inheritance hierarchies, the default upcast behavior might not work. Consider for example:

struct A {};
struct B : A {};
struct C : A {};
struct D : B, C {};

In that hierarchy an object of type D has two different bases of type A. The conversion from D* to A* is ambiguous. For this type of cases you can force going through an intermediate step

A *p = static_cast<B*>(d);   // Give me the 'A' subobject within 'B'

Similarly, if there is a function with multiple overloads that can take either the base or the derived type and you need to call the version taking the base type (note: code smell here!) then you can use the cast to direct overload resolution. Again, if you need to direct overload resolution, you are probably doing something else wrong.

Other than corner cases (i.e. when the default upcast does not work) there is really no reason to explicitly use a cast operation, and I would actually advice against it, as casts should be a warning light and overusing them might make them normal and not raise alarms when reading code.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • 1
    Good corner case example but I’d question its practical relevance since this is bad code that should never occur in your code base anyway (diamond of death) – at least in combination with an upcast. – Konrad Rudolph Apr 29 '13 at 19:00
1

In case of multiple inheritance (which Java doesn't really have) you will possibly adjust your "this" pointer. In that case a reinterpret_cast is not safe, which is what they are trying to emphasize by saying that static_cast and dynamic_cast are safe.

As a small note, in an investigation in a codebase that was to be ported to Android-without-dynamic_cast I found 127 dynamic_casts, of which 122 were not actually necessary. This was code written by people that were OK to very proficient in C++.

dascandy
  • 7,184
  • 1
  • 29
  • 50
0

Why are these even required for upcasting?

They aren’t, and using them for upcasts is (IMHO) misleading: no cast is required for a reason: logically, a subclass object is-an instance of the superclass.

In summary, I would discourage explicit upcasts. For people who insist on that I demand consistency at least: use explicit upcasts everywhere, including in initialisations:

A * ptr{static_cast<A*>(new B)};

Most people will agree that this is just silly.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Thats what I thought. But then why does an answer with 673 upvotes say "You don't have to use it to cast downwards" about dynamic casts http://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-and-reinterpret-cast-be-used. Didn't post on that thread since I thought there is something fundamentally wrong with my understanding – user929404 Apr 29 '13 at 18:56
  • 1
    @user929404 You are misinterpreting that answer – the actual message is that `reinterpret_cast` does *not only* work for downcasts, it can also perform other casts. However, the posting says nothing about actually *having* to use it for upcasts (other online comments, however, *do* advise this). – Konrad Rudolph Apr 29 '13 at 18:58