virtual inheritance and traits technique

M

mathieu

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 ‘CC<MyTrait>’:
virtual4.cxx:37: instantiated from ‘MyC<MyTrait>’
virtual4.cxx:53: instantiated from here
virtual4.cxx:9: error: no unique final overrider for ‘virtual void
Common::f()’ in ‘CC<MyTrait>’
virtual4.cxx:9: error: no unique final overrider for ‘virtual void
Common::g()’ in ‘CC<MyTrait>’
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top