LWN.net Logo

GCC 4.6.0 released

GCC 4.6.0 released

Posted Apr 7, 2011 16:42 UTC (Thu) by daglwn (subscriber, #65432)
In reply to: GCC 4.6.0 released by cmccabe
Parent article: GCC 4.6.0 released

Mixins do not have to implement anything. Again, you're artificially restricting what's possible. It's sometimes convenient to use mixins to convey relationships among types. I have a mother and a father. My mother has a mother and a father. So does my father. That means my maternal-side cousin and I have grandparents in common and my paternal-side cousin and I also have grandparents in common, but a different set.

In more concrete terms, an add expression is a binary operator. So is a subtract expression. Both are also associative, but only one is commutative. That's a useful thing to express in class hierarchies and using mixins is a great way to do it.


(Log in to post comments)

GCC 4.6.0 released

Posted Apr 8, 2011 17:57 UTC (Fri) by cmccabe (guest, #60281) [Link]

from Wikipedia:

> In object-oriented programming languages, a mixin is a class that provides
> a certain functionality to be inherited by a subclass, while not meant for
> instantiation (the generation of objects of that class). Inheriting from a
> mixin is not a form of specialization but is rather a means of collecting
> functionality.

So it's not about relationships between types. It is about code reuse.

Bruce Eckel suggests a way of implementing mixins in C++ using templates:

http://www.artima.com/weblogs/viewpost.jsp?thread=132988

I would suggest yet another: private inheritance.

GCC 4.6.0 released

Posted Apr 8, 2011 18:35 UTC (Fri) by daglwn (subscriber, #65432) [Link]

I've used CRTP as well and find it useful. I also find mixins as relationship useful. I my mind, inheritance is about relationships. For functionality, I would agree with GoF and you that composition is generally a better path. I very rarely use private inheritance. I honestly can't think of a situation where I've found it absolutely necessary.

But in any case, I think the discussion leads us to a place where general MI is useful, even with the pitfalls. Pointers in C have a similar usefulness/pitfall tradeoff. I like the fact the C++ is flexible like this. I wish it were more flexible than it is! :)

GCC 4.6.0 released

Posted Apr 11, 2011 0:30 UTC (Mon) by cmccabe (guest, #60281) [Link]

Well, as I said before, I think being able to inherit from multiple (fully) abstract base classes is useful. I have never found a good use for inheriting from multiple non-abstract base classes. I guess we'll have to agree to disagree.

Anyway, language design is an endless process. It really comes down to who what languages are most successful in the marketplace, and in the open source world. My philosophy is to use the right tool for the job.

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds