65,211
社区成员
发帖
与我相关
我的任务
分享
class Strategy11
{
public:
typedef char KEY;
};
class Strategy12
{
public:
typedef unsigned short KEY;
};
class Strategy21
{
public:
static int Foo(){ return 10;}
};
class Strategy22
{
public:
static int Foo(){ return 20;}
};
class RootClass
{
public:
virtual int Test1() {return -1;};
virtual int Test2() {return -1;};
};
template <class TypeStrategy,class BehaviorStrategy>
class BaseClass : public RootClass
{
public:
typedef typename TypeStrategy::KEY KEY;
int Test1() { return sizeof(KEY); };
int Test2() { return BehaviorStrategy::Foo(); };
};
typedef BaseClass<Strategy11,Strategy21> MyStrategy;
typedef BaseClass<Strategy12,Strategy22> YourStrategy;
//////////////////////////////////////////////////
void Test()
{
MyStrategy d1;
int n1 = d1.Test1();
int n2 = d1.Test2();
YourStrategy d2;
int n3 = d2.Test1();
int n4 = d2.Test2();
RootClass *p = &d1;
int n5 = p->Test1();
int n6 = p->Test2();
p = &d2;
int n7 = p->Test1();
int n8 = p->Test2();
}
int main()
{
Test();
}
void main()
{
BaseClass <MyStrategy> d1;
int n1 = d1.Test1();
int n2 = d1.Test2();
BaseClass <YourStrategy> d2;
int n3 = d2.Test1();
int n4 = d2.Test2();
RootClass *p = &d1;
int n5 = p->Test1();
int n6 = p->Test2();
p = &d2;
int n7 = p->Test1();
int n8 = p->Test2();
#define check(op, result) \
cout << #op << "(" << op << ")" << "\t" << " = " << result << "\t"\
<< "\t" << (((op) == result) ? "pass" : "failed" ) << endl
check(n1, 1);
check(n2, 10);
check(n3, 2);
check(n4, 20);
check(n5, 1);
check(n6, -1);
check(n7, 2);
check(n8, -1);
}
class MyStrategy : public Strategy11, public Strategy21
{
};
class YourStrategy : public Strategy12, public Strategy22
{
};
template<class G1, class G2>
class State_T : public G1, G2
{};
typedef State_T<Strategy11, Stategy21> MyStrategy;