1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#include <iostream>
template<class aTraitType,class bTraitType>
class A_base;
template<class aTraitType,class bTraitType>
class B_base;
template<class aTraitType,class bTraitType>
class A_base
: public virtual aTraitType
{
private:
B_base<aTraitType,bTraitType>* myB;
public:
A_base(B_base<aTraitType,bTraitType>* myB_in=NULL)
: myB(myB_in)
{}
void setB(B_base<aTraitType,bTraitType>* myB_in)
{
myB=myB_in;
}
B_base<aTraitType,bTraitType>* getB()
{
return myB;
}
};
template<class aTraitType,class bTraitType>
class B_base
: public virtual bTraitType
{
private:
A_base<aTraitType,bTraitType>* myA;
public:
B_base(A_base<aTraitType,bTraitType>* myA_in=NULL)
: myA(myA_in)
{}
void setA(A_base<aTraitType,bTraitType>* myA_in)
{
myA=myA_in;
}
A_base<aTraitType,bTraitType>* getA()
{
return myA;
}
};
class A_traits
{
public:
virtual void print(unsigned long depth=0)=0;
};
class B_traits
{
public:
virtual void print(unsigned long depth=0)=0;
};
template<class aTraitType,class bTraitType>
class A
: public A_base<aTraitType,bTraitType>
{
public:
void print(unsigned long depth=0)
{
if(depth==0)
std::cout << "Class A at " << this << " reporting for duty" << std::endl;
else if(this->getB()!=NULL)
this->getB()->print(depth-1);
else
std::cout << "Class A is not connected to B" << std::endl;
}
};
template<class aTraitType,class bTraitType>
class B
: public B_base<aTraitType,bTraitType>
{
public:
void print(unsigned long depth=0)
{
if(depth==0)
std::cout << "Class B at " << this << " reporting for duty" << std::endl;
else if(this->getA()!=NULL)
this->getA()->print(depth-1);
else
std::cout << "Class B is not connected to A" << std::endl;
}
};
int main()
{
A<A_traits,B_traits> aObj;
B<A_traits,B_traits> bObj;
aObj.print(1);
bObj.print(1);
aObj.setB(&bObj);
bObj.setA(&aObj);
aObj.print();
bObj.print();
aObj.print(1);
bObj.print(1);
aObj.print(2);
bObj.print(2);
}
|