virtual inheritance and traits technique

Discussion in 'C++' started by mathieu, Apr 14, 2010.

  1. mathieu

    mathieu Guest

    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>’
     
    mathieu, Apr 14, 2010
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    19
    Views:
    812
    Daniel T.
    Feb 4, 2004
  2. cppsks
    Replies:
    0
    Views:
    832
    cppsks
    Oct 27, 2004
  3. Ashwin
    Replies:
    2
    Views:
    356
    Pierre Barbier de Reuille
    Aug 1, 2006
  4. Replies:
    1
    Views:
    476
    Alf P. Steinbach
    Mar 14, 2009
  5. ray
    Replies:
    1
    Views:
    1,363
    Robert Kern
    Jun 4, 2010
Loading...

Share This Page