Template -- Diamond ring Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pallav singh

    #1

    Template -- Diamond ring Problem

    I am Facing Problem while creating object of Diamond Ring problem
    solving using Template
    Kindly let me known where i am committing ERROR

    Thanks
    Pallav

    #include<iostre am.h>
    #include<string .h>

    template<typena me T>
    class A
    {
    private :
    T a;

    public :
    A(T a1):a(a1){}
    A(const A<T>& a){}

    ~A(){}

    void Get_Value()cons t
    {cout<<"Value of a :: "<< a <<endl<<"\n"; }

    };


    template<typena me T ,typename U>
    class B :virtual public A<T>
    {
    private :
    U b;
    U c;

    public :

    B(T a1,U b1):A<T>(a1),b( b1),c(b1){}
    B(const B<T,U>& b){}

    ~B(){}

    void Show_Value_B()c onst
    { cout<<"Value of B___ b is :: "<<b <<endl<<"\n";
    cout<<"Value of B___ c is :: "<<c <<endl<<"\n";
    }
    };

    template<typena me T ,typename V>
    class C :virtual public A<T>
    {
    private :
    V b;
    V c;
    public :

    C(T a1,V b1):A<T>(a1),b( b1),c(b1){}
    C(const C<T,V>& c){}

    ~C(){}

    void Show_Value_C()c onst
    { cout<<"Value of C___ b is :: "<<b <<endl<<"\n";
    cout<<"Value of C___ c is :: "<<c <<endl<<"\n";
    }
    };


    template<typena me T,typename U ,typename V>
    class D :virtual public B<T,U>, virtual public C<T,V>
    {
    public :
    D(T a,U b,V c):B<T,U>(a,b), C<T,V>(a,c){}
    D(const D<T,U,V&d){}
    ~D(){};
    };


    int main(int argc , char *argv[])
    {
    D<int,char,floa tb(12,'V',17.8) ;
    b.Get_Value();
    b.Show_Value_C( );
    b.Show_Value_B( );

    return 0;
    }
  • Salt_Peter

    #2
    Re: Template -- Diamond ring Problem

    On Dec 27, 2:55 am, Pallav singh <singh.pal...@g mail.comwrote:
    I am Facing Problem while creating object of Diamond Ring problem
    solving using Template
    Kindly let me known where i am committing ERROR
    read:
    [25.9] Where in a hierarchy should I use virtual inheritance?

    >
    Thanks
    Pallav
    >
    #include<iostre am.h>
    #include<string .h>
    #include <iostream>
    #include <string>
    >
    template<typena me T>
    class A
    {
    private :
    T a;
    >
    public :
    A(T a1):a(a1){}
    A(const T a1) : a(a1) { }
    or
    A(const T& t) : a(t) { }

    If T is any primitive type, like integer, the constructor will work
    well with the first case.
    If T were a complex user-type, a const reference is much, much better.

    I use the second case exclusively unless its specifically prohibited.
    In your case, you might prefer an explicit ctor.
    A(const A<T>& a){}
    >
    ~A(){}
    >
    void Get_Value()cons t
    {cout<<"Value of a :: "<< a <<endl<<"\n"; }
    void Get_Value() const
    {
    std::cout << "Value of a :: " << a;
    std::cout << std::endl << std::endl;
    }

    although that function should be a friend operator<< overload
    What if T is not a primitive type?
    >
    };
    >
    template<typena me T ,typename U>
    class B :virtual public A<T>
    template < typename T ,typename U >
    class B : public virtual A< T >
    {
    private :
    U b;
    U c;
    >
    public :
    >
    B(T a1,U b1):A<T>(a1),b( b1),c(b1){}
    B(const B<T,U>& b){}
    >
    ~B(){}
    >
    void Show_Value_B()c onst
    { cout<<"Value of B___ b is :: "<<b <<endl<<"\n";
    cout<<"Value of B___ c is :: "<<c <<endl<<"\n";
    }
    };
    >
    template<typena me T ,typename V>
    class C :virtual public A<T>
    {
    private :
    V b;
    V c;
    public :
    >
    C(T a1,V b1):A<T>(a1),b( b1),c(b1){}
    C(const C<T,V>& c){}
    >
    ~C(){}
    >
    void Show_Value_C()c onst
    { cout<<"Value of C___ b is :: "<<b <<endl<<"\n";
    cout<<"Value of C___ c is :: "<<c <<endl<<"\n";
    }
    };
    B and C should really be one class
    >
    template<typena me T,typename U ,typename V>
    class D :virtual public B<T,U>, virtual public C<T,V>
    Don't bother trying to construct a mesh, deal with a diamond first.
    B and C are not virtual.
    If they were, you wouldn't be able to generate an instance of D.

    template < typename T, typename U, typename V>
    class D : public B< T, U >, public C< T, V >
    {
    public :
    D(T a,U b,V c):B<T,U>(a,b), C<T,V>(a,c){}
    D(const T& a, const U& b, const V& c)
    : A< T >(a), // somebody has to do it
    B< T, U >(a,b),
    C< T, V >(a,c)
    {}


    D(const D<T,U,V&d){}
    ~D(){};
    };
    >
    int main(int argc , char *argv[])
    {
    D<int,char,floa tb(12,'V',17.8) ;
    b.Get_Value();
    b.Show_Value_C( );
    b.Show_Value_B( );
    if you had op<< available for each class, you could display the entire
    mess with one line.
    std::cout << b << std::endl;
    >
    return 0;
    >
    }

    Comment

    • Pavel Shved

      #3
      Re: Template -- Diamond ring Problem

      On 27 ÄÅË, 12:25, Salt_Peter <pj_h...@yahoo. comwrote:
      On Dec 27, 2:55 am, Pallav singh <singh.pal...@g mail.comwrote:
      >
      void šGet_Value() const
      {
      š std::cout << "Value of a :: š" << a;
      š std::cout << std::endl << std::endl;
      >
      }
      >
      although that function should be a friend operator<< overload
      What if T is not a primitive type?
      Exactly what we're trying to achieve: print T in the way we don't know
      yet but require to exist. That's called `generic programming', ever
      heard of that?
      Don't bother trying to construct a mesh, deal with a diamond first.
      B and C are not virtual.
      If they were, you wouldn't be able to generate an instance of D.
      What are you talking about? oO If that's the reason why we can't
      generate instance of D we couldn't generate instances of B and C as
      well then! Let alone that the code above (with a little fixup to D's
      constructor) compiles and runs correctly.

      š š šD(const T& a, const U& b, const V& c)
      š š š š š š š š š š š š š š : A< T >(a), // somebody has to do it
      imho this doesn't catch the problem. `Somebody' has already `done' it,
      even two times (B and C constructors)!

      Comment

      • Salt_Peter

        #4
        Re: Template -- Diamond ring Problem

        On Dec 27, 4:46 am, Pavel Shved <Pavel.Sh...@gm ail.comwrote:
        On 27 ÄÅË, 12:25, Salt_Peter <pj_h...@yahoo. comwrote:
        >
        On Dec 27, 2:55 am, Pallav singh <singh.pal...@g mail.comwrote:
        >
        void šGet_Value() const
        {
        š std::cout << "Value of a :: š" << a;
        š std::cout << std::endl << std::endl;
        >
        }
        >
        although that function should be a friend operator<< overload
        What if T is not a primitive type?
        >
        Exactly what we're trying to achieve: print T in the way we don't know
        yet but require to exist. That's called `generic programming', ever
        heard of that?
        Uhuh, generic programming supposes that T be printable. What if T is
        not a primitive type or a std::string, etc.
        So i'll throw your statement right back at you: have *YOU* ever heard
        of generic programming?
        (think: T must provide an op<<)
        >
        Don't bother trying to construct a mesh, deal with a diamond first.
        B and C are not virtual.
        If they were, you wouldn't be able to generate an instance of D.
        >
        What are you talking about? oO If that's the reason why we can't
        generate instance of D we couldn't generate instances of B and C as
        well then! Let alone that the code above (with a little fixup to D's
        constructor) compiles and runs correctly.
        Thats my mistake
        >
        š š šD(const T& a, const U& b, const V& c)
        š š š š š š š š š š š š š š : A< T >(a), // somebody has to do it
        >
        imho this doesn't catch the problem. `Somebody' has already `done' it,
        even two times (B and C constructors)!
        not if you remove those constructors in B and C, kindof obvious.
        Or was i suppose to do what you didn't do - remove A's ctors in B and
        C?
        Please

        Comment

        • Pavel Shved

          #5
          Re: Template -- Diamond ring Problem

          On 27 дек, 21:58, Salt_Peter <pj_h...@yahoo. comwrote:
          On Dec 27, 4:46 am, Pavel Shved <Pavel.Sh...@gm ail.comwrote:
          >
          >
          >
          On 27 ÄÅË, 12:25, Salt_Peter <pj_h...@yahoo. comwrote:
          >
          On Dec 27, 2:55 am, Pallav singh <singh.pal...@g mail.comwrote:
          >
          void šGet_Value() const
          {
          Å¡ std::cout << "Value of a :: Å¡" << a;
          Å¡ std::cout << std::endl << std::endl;
          >
          }
          >
          although that function should be a friend operator<< overload
          What if T is not a primitive type?
          >
          Exactly what we're trying to achieve: print T in the way we don't know
          yet but require to exist.  That's called `generic programming', ever
          heard of that?
          >
          Uhuh, generic programming supposes that T be printable. What if T is
          not a primitive type or a std::string, etc.
          Well, primitive and STL types are not only ones who provide printing.
          That's wat i was talking about.
          Now i should also mention that yielding a compile-error if T does not
          statisfy concept requirements is what STL types do. The OP's class
          would do the same.
          So, back to original question, `What is if T is not a primitive type?'
          - nothing special. OP's written the very thing he should do in that
          particular line with printing statement.

          š š šD(const T& a, const U& b, const V& c)
          Å¡ Å¡ Å¡ Å¡ Å¡ Å¡ Å¡ Å¡ Å¡ Å¡ Å¡ Å¡ Å¡ Å¡ : A< T >(a), // somebody has to do it
          >
          imho this doesn't catch the problem. `Somebody' has already `done' it,
          even two times (B and C constructors)!
          >
          not if you remove those constructors in B and C, kindof obvious.
          It seems i dont understand you. Whom did you mean by `somebody'?
          Constructor of D? But he can not be referred to as SOMEbody because
          he's an only CORRECT one to do it, and that's whai i've been talking
          about.
          Or was i suppose to do what you didn't do - remove A's ctors in B and
          C?
          I wasn't even going to do this as OP's problem is with D class, all
          the other classes being absolutely correct.

          Comment

          Working...