I did it, but it's terrible. I really love templates.
template<class T> class Base1 { }; template<class T> class Base2 { }; class Derived; template<> class Base1<Derived> { public: double foo(){return 0.1;} }; template<> class Base2<Derived> { public: int foo(){return 1;} }; class Derived : public Base1<Derived> , public Base2<Derived> { public: using Base1<Derived>::foo; }; int main() { double sum = 0; Derived d; sum += d.foo();
I could not get it to work without templates, although it seems that it should be able to. I'm not sure you can just perform the template member functions in the same way, but my gut says no.
In terms of organizing the code, I would then define Base # material immediately after defining or declaring Derived, as that is really what it is for. Keep in mind that you can use typename Base1<Derived> something to make things typename Base1<Derived> something prettier.
Edit: Oh right! This does not allow you to use the βuseβ trick or have different return types, but otherwise easier:
class Derived : public Base1 , public Base2 { double Base1::foo(){...} double Base2::foo(){...} }
It may be a terrible, terrible, amazing way to combine these two approaches, but I donβt think it really helps when using the code. I can come back to you.
Narfanator Jan 02 '09 at 4:34
source share