Question

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 ?

Was it helpful?

Solution

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.”

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top