Ah ? Mais si tu veux que toutes tes classes implémentent quelque chose comme GetTypeName().
Perso j'ai une classe IObject dont 99 % des classes de mon projet dérivent. Au début ça m'a fait bizarre, mais c'est très pratique. Bonne combinaison avec ma factory et tout ce qui va avec...
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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 #ifndef OBJECT_H #define OBJECT_H #include <string> #include <boost/shared_ptr.hpp> #define DECLARE_TYPE(Class) \ virtual std::string GetTypeName() const {return #Class; } #define DECLARE_CLONE(Class) \ virtual boost::shared_ptr<IObject> Clone() const {return boost::shared_ptr<IObject>(new Class(*this)); } //----------------------------------------------------------- // IObject : Base class //----------------------------------------------------------- struct IObject { IObject() {} virtual ~IObject() {} // Fonction to get the real type of the Class. virtual std::string GetTypeName () const = 0; // Polymorphic copy the object virtual boost::shared_ptr<IObject> Clone () const { throw std::exception("This object is no clonable"); } }; typedef boost::shared_ptr<IObject> object_ptr; #endif
Partager