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
|
#include <iostream>
using namespace std;
class D1;
class D2;
class B
{
public:
B & operator=(const B & b)
{
if (type()!=b.type())
{
cerr<< "type mismatch in assignment operation\n";
return *this;
}
return *( (B*) (AF[type()](this,&b)) );
}
protected:
virtual int type()const {return 0;}
static B * assignB(B * dest,const B * source);
static D1 * assignD1(D1 * dest,const D1 * source);
static D2 * assignD2(D2 * dest,const D2 * source);
typedef void * (*assignment_func)(void*,const void*);
static const assignment_func AF[3];
private:
int val;
};
const B::assignment_func B::AF[3]={
(assignment_func)assignB,
(assignment_func)assignD1,
(assignment_func)assignD2};
class D1:public B
{
public:
D1 & operator=(const B & b){return (D1&)((B&)*this=b);}
private:
int val1;
int type()const {return 1;}
friend D1 * B::assignD1(D1 * dest,const D1 * source);
};
class D2:public B
{
public:
D2 & operator=(const B & b){return (D2&)((B&)*this=b);}
private:
int val2;
int type()const {return 2;}
friend D2 * B::assignD2(D2 * dest,const D2 * source);
};
B * B::assignB(B * dest,const B * source)
{
cout << "Base=Base" << endl;
dest->val=source->val;
return dest;
}
D1 * B::assignD1(D1 * dest,const D1 * source)
{
cout << "Derived1=Derived1" << endl;
dest->val=source->val;
dest->val1=source->val1;
return dest;
}
D2 * B::assignD2(D2 * dest,const D2 * source)
{
cout << "Derived2=Derived2" << endl;
dest->val=source->val;
dest->val2=source->val2;
return dest;
}
int main()
{
B b;
D1 d1;
D2 d2;
B * array[]={&b,&d1,&d2};
int i,j;
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
*array[i]=*array[j];
}
cout << endl;
b=b;
d1=d1;
d2=d2;
b=d1;
d2=b;
d1=d2;
system("pause");
return 0;
}
|