【c++テンプレートでポリシーモードを実現する】chjavachポリシーモードを見て感銘を受ける
#include "stdafx.h"
#include <vector>
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef map<string,int> vint;
// !
template<class T>
bool output(T _t){
return _t.out();
}
// 1
class A
{
public:
bool out(){ printf("i am an A !
"); return false; };
};
// 2
class B
{
public:
bool out(){printf("i am an B!
"); return true;};
};
//
void outcontext(){
A a1;
B b1;
if (!output(a1))
{
output(b1);
}
}
int main(int argc, char* argv[])
{
outcontext();
return 0;
}
テンプレートメソッドモードに統一できるかもしれません!