Why can't structs be derived from?

Jonathan M Davis jmdavisProg at gmx.com
Wed Mar 16 16:32:00 PDT 2011


On Wednesday, March 16, 2011 16:16:15 KennyTM~ wrote:
> On Mar 16, 11 23:01, Ary Manzana wrote:
> > On 3/15/11 3:29 PM, Andrei Alexandrescu wrote:
> >> On 3/15/11 12:55 PM, Jens wrote:
> >>> Steven Schveighoffer wrote:
> >>>> That's all there is. Structs do not have inheritance, only alias
> >>>> this.
> >>> 
> >>> Why don't they though? Inheritance does not have to mean polymorphic.
> >>> It can mean composition, like in C++. I don't understand the reason
> >>> for such
> >>> ugly syntax.
> >> 
> >> Using inheritance for composition is frowned upon in C++ for good
> >> reasons. If you want composition, the best is to use composition.
> >> 
> >> The reason for the allegedly ugly syntax is that it's considerably more
> >> general. It is often the case that a struct defines an entity that is
> >> implicitly convertible to another entity - could be an rvalue vs.
> >> lvalue, a class vs. another struct vs. a primitive type, could need a
> >> run-time operation etc. Inheritance would offer at best few of these
> >> amenities, whereas 'alias this' offers all with a simple syntax.
> >> 
> >> 
> >> Andrei
> > 
> > Syntax matters. A lot. Which one is more readable/understandable?
> > 
> > struct Point2 {
> > int x;
> > int y;
> > }
> > 
> > 1.
> > 
> > struct Point3 {
> > Point2 point2;
> > alias this point2;
> 
> Should be "alias point2 this;"
> 
> (strengthening the argument that this syntax is not good?)

It's no worse than C++'s typedef in that regard, and it becomes instantly 
obviously when you try and compile it, so I don't think that it's ultimately all 
that much of an issue.

- Jonathan M Davis


More information about the Digitalmars-d mailing list