-2

Suppose I have the following two statements

base* b = ...;
fnl * c = dynamic_cast<fnl*>(b); //Statement A
fnl& d = dynamic_cast<fnl&>(*b); //Statement B

I wanted to know exactly what the difference between statement A and statement B is. I understand that statement A casts and returns a pointer while statement B returns a reference. In case of A is it upto the user to free memory of c ?

Rajeshwar
  • 11,179
  • 26
  • 86
  • 158

1 Answers1

3

First call will return NULL if cast fails. Second call will throw an exception.

There are never any additional allocations going on, so nothing to free. That is, there is only one object that is being referenced by 3 variables now. You can only free it once. Technically it doesn't matter which variable you are using, but try to avoid confusion for sanity sake.

Eugene
  • 7,180
  • 1
  • 29
  • 36