#include <iostream>
#include <vector>
using namespace std;
inline void print(char *msg)
...{
cout << " " << msg << endl;
}
class A
...{
public:
virtual void foo1()
...{
print("A:v_foo1");
}
void foo2()
...{
print("A:foo2");
}
};
class B : public A
...{
public:
void foo1()
...{
print("B:v_foo1");
}
void foo2()
...{
print("B:foo2");
}
};
class C : public A
...{
public:
void foo1()
...{
print("C:v_foo1");
}
};
class D : public A
...{
public:
void foo2()
...{
print("D:foo2");
}
};
template<class T> void test1()
...{
A * a = new T();
T * b = (T*)a;
b->foo1();
b->foo2();
a->foo1();
a->foo2();
delete a;
}
template<class T> void test2()
...{
T *b = new T();
b->foo1();
b->foo2();
A *a = b;
a->foo1();
a->foo2();
delete b;
}
template<class T> void test3()
...{
T b;
b.foo1();
b.foo2();
((A)b).foo1();
((A)b).foo2();
}
template<class T> void test4()
...{
T b;
b.foo1();
b.foo2();
A *a = &b;
a->foo1();
a->foo2();
}
template<class T> void test5()
...{
A a;
T* b = (T*)&a;
b->foo1();
b->foo2();
a.foo1();
a.foo2();
}

/**//**
* 测试对象
*/
class Test
...{
public:
Test(char *msg)
...{
msg_ = msg;
}
Test(char *msg, void (*fun)())
...{
msg_ = msg;
fun_ = fun;
}
virtual ~Test()
...{
}
virtual void run()
...{
outMessage();
if (fun_)
fun_();
}
void outMessage()
...{
cout << msg_ << endl;
}
protected:
char *msg_;
private:
void (*fun_)();
};

/**//**
* 测试对象集
*/
class TestSet : public Test
...{
public:
TestSet(char *msg) :
Test(msg)
...{
}
virtual ~TestSet()
...{
vector<Test *>::iterator t_iterator;
for (t_iterator=tests.begin(); t_iterator!=tests.end(); t_iterator++)
...{
delete (*t_iterator);
}
}
virtual void run()
...{
outMessage();
vector<Test *>::iterator t_iterator;
for (t_iterator=tests.begin(); t_iterator!=tests.end(); t_iterator++)
...{
(*t_iterator)->run();
}
}
void addTest(Test *t)
...{
tests.push_back(t);
}
private:
vector<Test *> tests;
};

/**//**
* 用于创建BCD测试对象
*/
template<class T> Test * createTestBCD(char *msg)
...{
TestSet *ts = new TestSet(msg);
ts->addTest(new Test("test1",&(test1<T>)));
ts->addTest(new Test("test2",&(test2<T>)));
ts->addTest(new Test("test3",&(test3<T>)));
ts->addTest(new Test("test4",&(test4<T>)));
ts->addTest(new Test("test5",&(test5<T>)));
return ts;
}
Test * createAllTest()
...{
TestSet *testSet = new TestSet("TEST Virtual:");
testSet->addTest(createTestBCD<B>("******Test B******"));
testSet->addTest(createTestBCD<C>("******Test C******"));
testSet->addTest(createTestBCD<D>("******Test D******"));
return testSet;
}
int main()
...{
Test * allTest = createAllTest();
allTest->run();
delete allTest;
system("PAUSE");
}
运行结果如下:
TEST Virtual:
******Test B******
test1
B:v_foo1
B:foo2
B:v_foo1
A:foo2
test2
B:v_foo1
B:foo2
B:v_foo1
A:foo2
test3
B:v_foo1
B:foo2
A:v_foo1
A:foo2
test4
B:v_foo1
B:foo2
B:v_foo1
A:foo2
test5
A:v_foo1
B:foo2
A:v_foo1
A:foo2
******Test C******
test1
C:v_foo1
A:foo2
C:v_foo1
A:foo2
test2
C:v_foo1
A:foo2
C:v_foo1
A:foo2
test3
C:v_foo1
A:foo2
A:v_foo1
A:foo2
test4
C:v_foo1
A:foo2
C:v_foo1
A:foo2
test5
A:v_foo1
A:foo2
A:v_foo1
A:foo2
******Test D******
test1
A:v_foo1
D:foo2
A:v_foo1
A:foo2
test2
A:v_foo1
D:foo2
A:v_foo1
A:foo2
test3
A:v_foo1
D:foo2
A:v_foo1
A:foo2
test4
A:v_foo1
D:foo2
A:v_foo1
A:foo2
test5
A:v_foo1
D:foo2
A:v_foo1
A:foo2


被折叠的 条评论
为什么被折叠?



