Hello,
J'ai besoin de créer un map de foncteurs, afin de me créer un objet en fonction d'une chaîne de caractères envoyée.
Ci-dessous figure un code avec, pour simplifier, des clés de type entier.
Tout fonctionne très bien, mais je n'arrive pas à faire une version dont le constructeur des arguments.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 template <typename T> T* create() { return new T(); } int main(int argc, char **argv) { const int PARENT=1; const int CHILD=2; using namespace std; typedef boost::function< Parent* () > Functor; map< int, Functor > map; map[PARENT] = &create<Parent>; map[CHILD] = &create<Child>; delete map[PARENT](); delete map[CHILD](); }
J'ai essayé
mais lorsque j'entre CHILD dans le map, j'obtiens l'erreur suivante :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 template <typename T> T* create(const char* toto) { return new T(toto); } int main(int argc, char **argv) { const int PARENT=1; const int CHILD=2; using namespace std; typedef boost::function< Parent* (const char*) > Functor; map< int, Functor > map; map[PARENT] = &create<Parent>; map[CHILD] = &create<Child>; delete map[PARENT](); delete map[CHILD](); }
error C2440: '='*: impossible de convertir de 'overloaded-function' en 'boost::function<Signature>'
Merci pour votre aide !
Partager