2

I wanted to know why the following does not work

 float f = 12;
 int& g = dynamic_cast<int&>(f);

I get the error

cannot dynamic_cast 'f' (of type 'float') to type 'int&' (target is not pointer or reference to class)

 int& g = dynamic_cast<int&>(f);

I know dynamic cast deals with both pointers and references so my question is do references only work for class types and not default types ?

Rajeshwar
  • 11,179
  • 26
  • 86
  • 158

1 Answers1

3

According to the C++ Standard (#5.2.7.1):

1 The result of the expression dynamic_cast(v) is the result of converting the expression v to type T. T shall be a pointer or reference to a complete class type, or “pointer to cv void.”

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335