8
void foo(MyClass* myClass)
{
    BaseClass* pBaseClass = dynamic_cast<BaseClass*>(myClass);
    delete myClass;   // <-------------- Does this affects on pBaseClass ?
}

In general how dynamic_cast actually works? (does it work like a copy constructor?)

Dinushan
  • 2,067
  • 6
  • 30
  • 47
  • If you do want a copy (and assuming BaseClass is actually a base class) then `BaseClass bc = *class;` – john Nov 20 '13 at 15:51
  • If `BaseClass` is actually a *base* class, then what is the point of that `dynamic_cast`??? Or any cast for that matter? – AnT stands with Russia Nov 20 '13 at 18:11
  • If you're worried that deleting a pointer to a base class might not free the memory properly see here: https://stackoverflow.com/questions/294927/does-delete-work-with-pointers-to-base-class – MasterHD Aug 02 '22 at 17:20

3 Answers3

7

No, that is not safe. dynamic_cast is just a type conversion - both the original and converted pointer point to the same object.

It is possible the converted pointer will point to a slightly different address (if multiple inheritance is involved), but it's still pointing (in)to the same object - no object copying occurs.

Edit: I mean "not safe" in the sense "after you delete myClass, pBaseClass is a dangling pointer." It is still legal code, though. Just quite dangerous.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • 1
    Why not? `dynamic_cast` either succeeds, then there exists an inheritance relationship which makes both the cast and calling `delete` legal (the correct destructor will be virtually dispatched). Or `dynamic_cast` returns `nullptr`, this is also safe. The standard defines `delete nullptr` as no-op. – Damon Nov 20 '13 at 16:09
  • @Damon Added an explanation; I had a strong impression that's the meaning of "safe" which the OP was after. – Angew is no longer proud of SO Nov 20 '13 at 16:24
  • 1
    Ah OK, I didn't consider one might try to still use the original pointer after deleting. Valid point :-) – Damon Nov 20 '13 at 16:26
6

(Note that class isn't a valid variable name, since it's a keyword. I'll call it c instead).

Is it safe to delete the pointer after dynamic_casting?

Yes; but beware that both pointers are invalid after deleting the object that they point to. You can't use either pointer value afterwards.

In general how dynamic_cast actually works?

It converts a pointer or reference to a class type into a pointer or reference to a different class type, with a run-time check that the conversion is valid. In this case, the cast will succeed (giving a valid pointer) if BaseClass is the same as, or a base class of, the dynamic type of the object. It will fail (giving a null pointer) otherwise.

If you were casting *c to a reference type, then failure would cause an exception (std::bad_cast), since there is no such thing as a null reference.

does it work like a copy constructor?

No. Copy constructors are for copying the object. This isn't copying it, just changing the type of a pointer that points to it. A copy would look like

BaseClass bc = *c;

Note that the type of bc is BaseClass, not the type of c (which is presumable a class derived from BaseClass); this is known as "slicing", since the derived part of the object is "sliced off" and not copied.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

Since you are dealing with pointers, it's only the value of the pointer that is copied (i.e, just the address).

Here's a decent explanation for similar code

C++ Does casting create a new object?

Community
  • 1
  • 1
theAlias
  • 396
  • 1
  • 14