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. Advertisements

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. Stuart Golodetz
    Replies:
    6
    Views:
    590
    Chris \( Val \)
    Aug 30, 2003
  2. qazmlp
    Replies:
    19
    Views:
    1,066
    Daniel T.
    Feb 4, 2004
  3. cppsks
    Replies:
    0
    Views:
    957
    cppsks
    Oct 27, 2004
  4. Martin Vorbrodt

    policy based design, and traits

    Martin Vorbrodt, Oct 14, 2005, in forum: C++
    Replies:
    4
    Views:
    530
    mlimber
    Oct 14, 2005
  5. Ashwin
    Replies:
    2
    Views:
    494
    Pierre Barbier de Reuille
    Aug 1, 2006
  6. Replies:
    1
    Views:
    580
    Alf P. Steinbach
    Mar 14, 2009
  7. ray
    Replies:
    1
    Views:
    1,810
    Robert Kern
    Jun 4, 2010
  8. Rouslan Korneychuk
    Replies:
    8
    Views:
    827
    Rouslan Korneychuk
    Feb 10, 2011
Loading...