| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Pallav singh" |
| Date: |
27 Dec 2007 01:55:13 AM |
| Object: |
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<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;
}
.
|
|
| User: "Pavel Shved" |
|
| Title: Re: Template -- Diamond ring Problem |
27 Dec 2007 02:35:13 AM |
|
|
On 27 =C4=C5=CB, 10:55, Pallav singh <singh.pal...@gmail.com> wrote:
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]
}
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.
.
|
|
|
| User: "Pallav singh" |
|
| Title: Re: Template -- Diamond ring Problem |
27 Dec 2007 02:52:17 AM |
|
|
On Dec 27, 1:35 pm, Pavel Shved <Pavel.Sh...@gmail.com> wrote:
On 27 =C4=C5=CB, 10:55, Pallav singh <singh.pal...@gmail.com> wrote:
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]
}
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.
++++++++++++++++++++++++++++++++++++++++++++=3D
Thanks a Lot
.
|
|
|
|
|
| User: "Salt_Peter" |
|
| Title: Re: Template -- Diamond ring Problem |
27 Dec 2007 03:25:37 AM |
|
|
On Dec 27, 2:55 am, Pallav singh <singh.pal...@gmail.com> wrote:
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 <string>
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< 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";
}
};
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>
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,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;
return 0;
}
.
|
|
|
| User: "Pavel Shved" |
|
| Title: Re: Template -- Diamond ring Problem |
27 Dec 2007 03:46:08 AM |
|
|
On 27 =C4=C5=CB, 12:25, Salt_Peter <pj_h...@yahoo.com> wrote:
On Dec 27, 2:55 am, Pallav singh <singh.pal...@gmail.com> wrote:
void =9AGet_Value() const
{
=9A std::cout << "Value of a :: =9A" << a;
=9A 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.
=9A =9A =9AD(const T& a, const U& b, const V& c)
=9A =9A =9A =9A =9A =9A =9A =9A =9A =9A =9A =9A =9A =9A : A< T >(a), // so=
mebody has to do it
imho this doesn't catch the problem. `Somebody' has already `done' it,
even two times (B and C constructors)!
.
|
|
|
| User: "Salt_Peter" |
|
| Title: Re: Template -- Diamond ring Problem |
27 Dec 2007 12:58:54 PM |
|
|
On Dec 27, 4:46 am, Pavel Shved <Pavel.Sh...@gmail.com> wrote:
On 27 =C4=C5=CB, 12:25, Salt_Peter <pj_h...@yahoo.com> wrote:
On Dec 27, 2:55 am, Pallav singh <singh.pal...@gmail.com> wrote:
void =9AGet_Value() const
{
=9A std::cout << "Value of a :: =9A" << a;
=9A 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
=9A =9A =9AD(const T& a, const U& b, const V& c)
=9A =9A =9A =9A =9A =9A =9A =9A =9A =9A =9A =9A =9A =9A : 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
.
|
|
|
| User: "Pavel Shved" |
|
| Title: Re: Template -- Diamond ring Problem |
27 Dec 2007 03:50:32 PM |
|
|
On 27 =D0=B4=D0=B5=D0=BA, 21:58, Salt_Peter <pj_h...@yahoo.com> wrote:
On Dec 27, 4:46 am, Pavel Shved <Pavel.Sh...@gmail.com> wrote:
On 27 =C3=84=C3=85=C3=8B, 12:25, Salt_Peter <pj_h...@yahoo.com> wrote:
On Dec 27, 2:55 am, Pallav singh <singh.pal...@gmail.com> wrote:
void =C5=A1Get_Value() const
{
=C5=A1 std::cout << "Value of a :: =C5=A1" << a;
=C5=A1 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. =C2=A0That's called `generic programming', eve=
r
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.
=C5=A1 =C5=A1 =C5=A1D(const T& a, const U& b, const V& c)
=C5=A1 =C5=A1 =C5=A1 =C5=A1 =C5=A1 =C5=A1 =C5=A1 =C5=A1 =C5=A1 =C5=A1 =
=C5=A1 =C5=A1 =C5=A1 =C5=A1 : 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.
.
|
|
|
|
|
|
|

|
Related Articles |
|
|