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
| #include <map>
template<class Obj,class TManager>class Object;
template<class Obj, class Key>
class manager
{
private :
std::map<Key,Object<Obj,manager<Obj,Key> > *> mon_map;
protected:
Object<Obj,manager<Obj,Key> > *GetKey(Key k_){
return mon_map[k_];
}
public:
template<class X, class Y>void methode(manager<X,Y> *x, Y y)
{
Object<Obj,manager<Obj,Key> > *obj=x->GetKey(y);
obj->p->do_it();
}
};
template <class T>
struct Identite
{
typedef T Type;
};
template<class Obj, class TManager>
class Object
{
public:
// pour gcc (MingGW :)
friend class Identite<TManager>::Type;
// pour Visual (VCExpress) :
friend typename Identite<TManager>::Type;
private :
Obj *p;
};
class A
{
public:
void do_it(){}
};
class B
{
public:
void do_it(){}
};
int main()
{
manager<A,int> m1;
manager<B,char> m2;
//m1.methode(&m2,'c'); provoque une erreur
m1.methode(&m1,1);
return 0;
} |
Partager