12

The pointer of derived class returned by new can be type cast to the pointer of its base class.

Is this true or false?

I know dynamic_cast can be used to cast downside. Generally, how to cast a pointer of derived class to a pointer of its base class?

avakar
  • 32,009
  • 9
  • 68
  • 103
skydoor
  • 25,218
  • 52
  • 147
  • 201
  • 2
    @Binary Worrier: Is "casted" even a word? I thought the past participle (or whatever) of "cast" is "cast". – C. K. Young Jan 28 '10 at 16:33
  • .. while we are at it, 'cast' suffices; 'type cast' (with or without the space) is treated as a sign of ignorance by some pedants. YMMV. ;) – dirkgently Jan 28 '10 at 16:59
  • @dirkgently: Perhaps he thought he could cast other things like functions, members, templates, you never know. – isekaijin Jan 28 '10 at 17:04

4 Answers4

15

Yes. Conversion from a pointer to a derived class to a pointer to a base class is implicit. Thus, the following is perfectly fine:

struct B { };
struct D : B { };

D* my_d_ptr = new D;
B* my_d_ptr_as_a_b_ptr = my_d_ptr;
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 5
    +1: however for clarity for the beginners out there, I'm just pointing out that the converse isn't true, and upcasts are inherently unsafe i.e. "B* b = new D();" is safe, but "D* d = new B()" is not. Thanks. – Binary Worrier Jan 28 '10 at 16:34
  • @Binary Worrier: Upcasts are very safe. Downcasts require `dynamic_cast`. (D -> B = upcast; B -> D = downcast) – C. K. Young Jan 28 '10 at 16:36
  • 1
    @Binary Worrier: True, but the OP seems already to know that ("I know dynamic_cast can be used to cast downside"). – James McNellis Jan 28 '10 at 16:37
  • I seem to have a different definition of "upcast" and "downcast" from everyone else :) Apologies for any confusion folks. – Binary Worrier Jan 28 '10 at 16:56
  • 2
    If you consider the inheritance tree to be "upside down," with base classes at the top and derived classes underneath, "up" and "down" make a bit more sense. – James McNellis Jan 28 '10 at 17:02
6

Casting a pointer to a derived to a pointer to base should be implicit. This is the whole point of polymorphism: An instance of a derived class should always be safely usable as an instance of the base class. Therefore, no explicit cast is necessary.

dsimcha
  • 67,514
  • 53
  • 213
  • 334
5

That's true if derived class inherits 'publicly' and 'non-virtually' from base:

You can't convert Derived* to Base* neither implicitly nor using static_cast/dynamic_cast (C-cast will do the job, but you should think twice before use this hack!);

class Base { };

class Derived : protected Base { };

int main()
{
   Base* b = new Derived(); // compile error
}

Also don't work if base class is ambiguous:

class Base { };

class Derived1 : public Base { };
class Derived2 : public Base { };

class MostDerived : public Derived1, Derived2 { };

int main()
{
   Base* b = new MostDerived(); // won't work (but you could hint compiler
                                // which path to use for finding Base
}

Edit: added code samples, added ambiguous use case, removed virtual inheritance example.

Alexander Poluektov
  • 7,844
  • 1
  • 28
  • 32
  • 1
    This answer seems confused. Do you mean you can't convert if the inheritance is private or virtual? – David Thornley Jan 28 '10 at 16:37
  • I think you mean "virtually" because your example is "non-virtually" and fails. MSDN: "when the specified base class is accessible and the conversion is unambiguous" – Karlis Olte Apr 21 '15 at 19:17
1

Your question is not clear becuse it mixes up several different things.

On the one hand, a pointer to derived class can be converted to a pointer to a base class (assuming the base is accessible). This is a natural conversion and there's no need for any cast to do it.

On the other hand, you mention dynamic_cast and its ability to to perform downcasts. But downcasts are casts in the opposite direction: from a pointer to a base class to a pointer to derived class. Downcasts can be performed by both dynamic_cast and static_cast depending on what you need and what amount of run-time checking you require.

... And at the same time you ar talking about casting the result of new, which, of course, can only be upcasted, but not downcasted.

So, what is it you are asking about? Upcasts or downcasts?

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765