Dillo v3.1.1-119-g140d9ebd
Loading...
Searching...
No Matches
identity.cc
Go to the documentation of this file.
1/*
2 * This small program tests how IdentifiableObject works with multiple
3 * inheritance ("diamond" inheritance, more precisely, since all
4 * classes have there root in IdentifiableObject.)
5 *
6 * Current status: With virtual superclasses, you get a class
7 * hierarchie "root -> A -> B -> C", so that the first part of this
8 * example works actually (C is a subclass of A and of B), but the
9 * second fails (it should print "false", but it is erroneously
10 * assumed that B is a subclass of A.)
11 */
12
13#include "lout/identity.hh"
14
15using namespace lout::identity;
16
17class A: virtual public IdentifiableObject
18{
19public:
20 static int CLASS_ID;
21 inline A () { registerName ("A", &CLASS_ID); }
22};
23
24class B: virtual public IdentifiableObject
25{
26public:
27 static int CLASS_ID;
28 inline B () { registerName ("B", &CLASS_ID); }
29};
30
31class C: public A, public B
32{
33public:
34 static int CLASS_ID;
35 inline C () { registerName ("C", &CLASS_ID); }
36};
37
38int A::CLASS_ID = -1, B::CLASS_ID = -1, C::CLASS_ID = -1;
39
40int main (int argc, char *argv[])
41{
42 printf ("A: %d, B: %d, C: %d\n", A::CLASS_ID, B::CLASS_ID, C::CLASS_ID);
43
44 C x;
45 assert (x.instanceOf (A::CLASS_ID));
46 assert (x.instanceOf (B::CLASS_ID));
47 assert (x.instanceOf (C::CLASS_ID));
48 printf ("x: %d\n", x.getClassId ());
49
50 B y;
51 printf ("y: %d; instance of A: %s\n",
52 y.getClassId (), y.instanceOf (B::CLASS_ID) ? "true" : "false");
53
54 return 0;
55}
int main(void)
Definition bookmarks.c:1613
Instances of classes, which are sub classes of this class, may be identified at run-time.
Definition identity.hh:99
void registerName(const char *className, int *classId)
This method must be called in the constructor for the sub class.
Definition identity.cc:83
Some stuff to identify classes of objects at run-time.
Definition identity.cc:25