| 12
 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
 
 | // RuleTest.cpp : Defines the entry point for the console application.
//
 
#include <iostream>
#include <map>
#include <string>
#include "stdafx.h"
 
class RuleType{
public:
    RuleType(int anId, std::string aName): id (anId), name (aName) {}
protected:
    int id;
    std::string name;
 
protected:
    RuleType();
    static std::map< int, RuleType*> rules;
 
public:
    virtual int getID() {return id;}
    virtual std::string getName() {return name;}
    static void insertRule(int i, RuleType* rule) {rules[i] = rule;}
    static RuleType * getRule(int i) {return rules[i];}
};
 
std::map<int, RuleType*> RuleType::rules;
 
 
class Rule1: public RuleType{
public:
    Rule1(int anId, std::string aName): RuleType(anId, aName) {}
 
private:
    Rule1();
    int idRule;
 
public:
    int performRule(int i) {return i;}
};
 
class Rule2: public RuleType{
public:
    Rule2(int anId, std::string aName): RuleType(anId, aName) {}
 
private:
    Rule2();
    int idRule;
 
public:
    std::string performRule(std::string str) {return str;}
};
 
int _tmain(int argc, _TCHAR* argv[])
{
    Rule1 * rule1 = new Rule1(1, "Rule1");
    Rule2 * rule2 = new Rule2(2, "Rule2");
    RuleType::insertRule(rule1->getID(), rule1);
    RuleType::insertRule(rule2->getID(), rule2);
 
    Rule1 * ruletmp1 = static_cast< Rule1 * > (RuleType::getRule(1));
    std::cout << "Rule1: " << ruletmp1->performRule(1) << std::endl;
 
    Rule2 * ruletmp2 = static_cast< Rule2 * > (RuleType::getRule(2));
    std::cout << "Rule2: " << ruletmp2->performRule("Rule2") << std::endl;
 
    int i;
    std::cin >> i;
	return 0;
} | 
Partager