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
|
#include "stdafx.h"
#include <iostream>
using namespace std;
struct IParent
{
public :
enum EType
{
eA,
eB
};
IParent(EType p_eType):m_eType(p_eType){};
EType GetType() const { return m_eType ;} ;
template<class T>
bool ISEqual(const T* p_Param) const
{
return false ;
};
protected :
EType m_eType ;
};
struct A : public IParent
{
A():IParent(eA){};
/*
template<class T>
bool ISEqual(const T* p_Param) const
{
return false ;
};
*/
template<>
bool ISEqual<A>(const A* p_Param) const
{
return true;
};
template<>
bool ISEqual<IParent> (const IParent* p_Param) const
{
return m_eType == p_Param->GetType();
};
};
struct B : public IParent
{
B():IParent(eB){};
template<class T>
bool ISEqual(const T* p_Param) const
{
return false ;
};
template<>
bool ISEqual<B>(const B* p_Param) const
{
return true ;
};
template<>
bool ISEqual<IParent> (const IParent* p_Param) const
{
return m_eType == p_Param->GetType();
};
};
int _tmain(int argc, _TCHAR* argv[])
{
A a ;
A a2 ;
B b ;
B b2 ;
IParent* a3 = new A();
IParent* b3 = new B();
char c ;
cout<<"a1 == a2 ? "<<a.ISEqual(&a2)<<endl;
cout<<"b1 == b2 ? "<<b.ISEqual(&b2)<<endl;
cout<<"a1 == b2 ? "<<a.ISEqual(&b2)<<endl;
cout<<"a1 == IPb3 ?"<<a.ISEqual(b3)<<endl;
cout<<"a1 == IPa3 ?"<<a.ISEqual(a3)<<endl;
cout<<"IPA3 == a1 ?"<<a3->ISEqual(&a)<<endl;
cin>>c ;
delete b3 ;
delete a3;
return 0;
} |
Partager