For the same reason
Shape* s1 = new Box; Box* b1 = s1;
not compiled. The compiler does not care that s1 refers to Box , and it does not have to worry.
If you know that s1 refers to Box , just say:
Box *s1 = new Box;
Syntax Note: Parsing Rules for Box * s1; (very simplified):
declaration := type-name declarator ; declarator := name | * declarator
therefore parsing:
Box * s1 ; ^^^^^^^^ declarator ^^^^^^^^^ ^^^^^^^^^^^^^^^^^^ type-name declarator
and Box (* (s1) ) grouping Box (* (s1) )
Considered the best writing style for Box *s1; because it is more consistent with parsing than Box* s1; If you declare more than one variable in a single declaration, the Box* syntax can be misleading:
Box* x, y;
x is a pointer to Box , but y is Box , because the parsing is:
Box (*x), y;
curiousguy
source share