[Cs3] Question on class example

Mikhail Nesterenko mikhail at cs.kent.edu
Sun Nov 6 21:46:13 EST 2016


> I found these code in example *bridgeFigures*:
> 
> Figure *userBox = new Square(size, ifFilled == 'f'?
>                                            *static_cast<Fill *>*(new
> Filled(fchar)):
>                                                  *static_cast<Fill *>*(new
> Hollow(fchar)) );
> 
> My question is: why automatic/implicit upper casting is not available in
> this case? Like this:
> 
>   Figure *userBox = new Square(size, ifFilled == 'f'?
>                                         *new Filled*(fchar): *new Hollow*
> (fchar));
> 

Square accepts class Fill as a parameter and even though Hollow and
Filled are derived classes, the compiler disallows pointer coercion
(implicit conversion). So the pointers need to be cast explicitly.

thanks,
---
Mikhail


More information about the cs3 mailing list