Template -- Diamond ring Problem

P

Pallav singh

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<iostream.h>
#include<string.h>

template<typename T>
class A
{
private :
T a;

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

~A(){}

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

};


template<typename 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()const
{ cout<<"Value of B___ b is :: "<<b <<endl<<"\n";
cout<<"Value of B___ c is :: "<<c <<endl<<"\n";
}
};

template<typename 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()const
{ cout<<"Value of C___ b is :: "<<b <<endl<<"\n";
cout<<"Value of C___ c is :: "<<c <<endl<<"\n";
}
};


template<typename 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,float> b(12,'V',17.8);
b.Get_Value();
b.Show_Value_C();
b.Show_Value_B();

return 0;
}
 
P

Pavel Shved

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<iostream.h>
#include<string.h>

Code:
}[/QUOTE]

Assume you wrote
 B(T a1,U b1):A<T>(a1),b(b1),c(b1){}
and
 C(T a1,V b1):A<T>(a1+1),b(b1),c(b1){}
                     ^^
                     check this out
What's the way you expect A to be constructed?  Your D class will have
exactly one instance of A (that's what virtual inheritance mean), but
will it be A(12) or A(13)?

Pre-order first-base walk?  It makes no sense.

That means you should explicitely tell compiler what particular
instance of A you should have in your D class.  Where? - the answer
bursts from within - in D's constructor:

 D(T a,U b,V c):A<T>(a),B<T,U>(a,b),C<T,V>(a,c){}
                ^^^^^^^
                that makes sense
                and compiles

What your code fails at is the inaccessibility of default A<int>
constructor in D's constructor.  Which makes sense after you read the
above.
 
P

Pallav singh

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<iostream.h>
#include<string.h>
Code:
[/QUOTE]

}[/QUOTE]

Assume you wrote
B(T a1,U b1):A<T>(a1),b(b1),c(b1){}
and
C(T a1,V b1):A<T>(a1+1),b(b1),c(b1){}
^^
check this out
What's the way you expect A to be constructed?  Your D class will have
exactly one instance of A (that's what virtual inheritance mean), but
will it be A(12) or A(13)?

Pre-order first-base walk?  It makes no sense.

That means you should explicitely tell compiler what particular
instance of A you should have in your D class.  Where? - the answer
bursts from within - in D's constructor:

D(T a,U b,V c):A<T>(a),B<T,U>(a,b),C<T,V>(a,c){}
^^^^^^^
that makes sense
and compiles

What your code fails at is the inaccessibility of default A<int>
constructor in D's constructor.  Which makes sense after you read the
above.[/QUOTE]

++++++++++++++++++++++++++++++++++++++++++++=
Thanks a Lot
 
S

Salt_Peter

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?
http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.9
Thanks
Pallav

#include<iostream.h>
#include<string.h>

#include <iostream>
#include said:
template<typename 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()const
{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<typename T ,typename U>
class B :virtual public A<T>

template < typename T ,typename U >
class B : public virtual A said:
{
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()const
{ cout<<"Value of B___ b is :: "<<b <<endl<<"\n";
cout<<"Value of B___ c is :: "<<c <<endl<<"\n";
}
};

template<typename 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()const
{ 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<typename 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>
{
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,float> b(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;
 
P

Pavel Shved

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)!
 
S

Salt_Peter

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<<)
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
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
 
P

Pavel Shved

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.

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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top