Making inheritance less tedious
Kristian Kilpi
kjkilpi at gmail.com
Fri Mar 2 04:45:55 PST 2007
On Fri, 02 Mar 2007 09:26:21 +0200, janderson <askme at me.com> wrote:
> janderson wrote:
>> Kevin Bealer wrote:
>>> I've often thought that in C++ I could achieve neat results by copying
>>> the vector,
>>> string, and map classes and adding my own functionality, but there is
>>> the
>>> annoyance of duplicating the half dozen or so string constructors.
>>> The simplest
>>> c++ syntax I have found is this (not tested):
>>>
>>> class MyString : string {
>>> public:
>>> template<A> MyString(A x) : string(x) {}
>>> template<A,B> MyString(A x, B y) : string(x,y) {}
>>> template<A,B,C> MyString(A x, B y, C z) : string(x,y,z) {}
>>> };
>> [snip]
>> This is where I think well designed mixins really start showing their
>> power. The problem in C++ is that the base classes are not written in
>> the mixin design pattern, probably because its yet another level of C++
>> complexity to add. In D mixins are easy, so theres is really no excuse.
>> Having said that I don't think they solve this problem entirely.
>> =Joel
>
> I should clarify, I'm talking about the template mixins, not the ones
> that take a string.
>
> -Joel
Yes, mixins are useful in some situation, as you said. However, if I need
to (quickly) change the behavior of a class by overriding a function or
two, mixins don't help: I have to write (usually) the constructors anyway.
If I'm writing a library, I could provide mixins for easier inheritance.
That's just a lot of unnecessary work and redundant code, and they make
the library harder to maintain.
More information about the Digitalmars-d
mailing list