0
votes

I was reading on this page and it says

dynamic_cast can be used only with pointers and references to objects. Its purpose is to ensure that the result of the type conversion is a valid complete object of the requested class.

But dynamic_cast doesn't give a valid object when I do this:

struct A{};
struct B:A{};
void main(){
    A a;
    B b;
    B* bPtr = (B*) 0x0000001;
    A* aPtr = dynamic_cast<A*>(bPtr);
    //Now aPtr is the memory address 0x00000001
}

Since the bPtr does not contain a valid A object, I would expect dynamic_cast to fail this cast so that aPtr would get NULL. But this is not happening? Which one is wrong, the compiler (g++ 4.5.7) or the above mentioned web page? Or am I missing something about what dynamic_cast is actually supposed to do?

Edit: Answer Summary

  1. In upcast, dynamic_cast does nothing. There is no guarantee of any sort.
  2. In downcast, dynamic_cast does not check for bad pointers. It assumes that the source pointer either is 0 or points to a valid source object. (the dynamic_cast operation can segfault during a downcast if it is given a bad pointer)
  3. Downcast through dynamic_cast requires at least one virtual member in the base class.
1
You're answer summary to paragraph 1 is wrong. If you cast a pointer to derived class to a pointer to an unambiguous base class the result is guaranteed to be a pointer to the valid base class of the derived object to which the souce pointer was pointing.CB Bailey
@CharlesBailey, I think in point-1, @neuron meant there is no RTTI happens while converting from Derived to base (as it's a trivial operation), for which the dynamic_cast<> is typically intended.iammilind
@iammilind: But it doesn't do "nothing" even in this trivial case. It will do the correct pointer adjustments to make the result valid.CB Bailey

1 Answers

2
votes

dynamic_cast<> is guaranteed to give a valid object, provided that it's used properly.

The very 1st requirement to use this cast is to have polymorphic base class (at least 1 virtual function). In your example, A and B are not polymorphic and it will result in compilation error.

The 2nd requirement is that dynamic_cast<> should be used for downcasting, in your case you are using it for upcast (which happens trivially).

Suppose, they were polymorphic then the 3rd requirement is to use this casting on a pointer/reference to a proper object.
bPtr = (B*)0x0000001 is not guaranteed to refer an object (mostly it's a UB) of B.

Here is the correct example:

struct A{ virtual ~A() {} };
struct B : A {};
void main(){
  A* pA = new B;
  B* pB = dynamic_cast<B*>(pA);
}