virtual inheritance and traits technique
Hi,
I am trying to use virtual inheritance when implementing a client
interface (that uses a traits technique). Here is the interface:
// Fixed API
template <typename T>
struct AA {
};
template <typename T>
struct BB {
};
template <typename T>
struct CC : public T::A , public T::B {
void specialfunc() {}
};
struct Trait {
typedef AA<Trait> A;
typedef BB<Trait> B;
typedef CC<Trait> C;
};
for this I provide my own subclasses. MyA, MyB and MyC are real
classes and can be instanciated (just like AA, BB and CC).
struct Common {
virtual void f() {}
virtual void g() = 0;
void h() {}
};
template <typename T>
struct MyA : public AA<T>, public virtual Common {
virtual void f() {}
virtual void g() {}
void callh() { this->h(); }
};
template <typename T>
struct MyB : public BB<T>, public virtual Common {
virtual void f() {}
virtual void g() {}
void callh() { this->h(); }
};
template <typename T>
struct MyC : public CC<T> {
virtual void f() {}
virtual void g() {}
void callh() { this->h(); }
};
struct MyTrait {
typedef MyA<MyTrait> A;
typedef MyB<MyTrait> B;
typedef MyC<MyTrait> C;
};
This does not work since the compiler requires that Common::f() &
Common::g() should be implemented in the client *own* class: CC.
I thought of making MyC inherits directly from MyA and MyB (to bypass
indirection), but I'll be loosing the `C::specialfunc()` function.
Any suggestion for this issue ? Thanks !
For anyone interested in this issue, code is at:
http://gdcm.svn.sf.net/viewvc/gdcm/Sandbox/virtual4.cxx
$ g++ virtual4.cxx
virtual4.cxx: In instantiation of =91CC<MyTrait>':
virtual4.cxx:37: instantiated from =91MyC<MyTrait>'
virtual4.cxx:53: instantiated from here
virtual4.cxx:9: error: no unique final overrider for =91virtual void
Common::f()' in =91CC<MyTrait>'
virtual4.cxx:9: error: no unique final overrider for =91virtual void
Common::g()' in =91CC<MyTrait>'