-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.cpp
More file actions
56 lines (39 loc) · 1.26 KB
/
test.cpp
File metadata and controls
56 lines (39 loc) · 1.26 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "Animal.hpp"
int main() {
// C Animal example
printf("\nC Animal example\n");
// Construct an Animal
Object* animal = Animal_create();
// Non-virtual method, cannot be overridden
Animal_pet(animal); // "You pet the animal."
// Virtual method
Animal_speak(animal); // "I'm an animal with 0 legs."
// We can specialize existing Objects even after they are created.
// If the object is already a Dog, this fails gracefully.
Dog_specialize(animal, "Dogbert");
// Call virtual setters
Dog_name_set(animal, "Fido");
Animal_legs_set(animal, 3);
// Call virtual method, which calls Dog's overridden implementation.
Animal_speak(animal); // "Woof, I'm a dog named Fido with 3 legs."
// All Objects must be unreferenced.
Object_unref(animal);
// C++ Animal proxy example
printf("\nC++ Animal proxy example\n");
Object* dog = Dog_create("Toto");
assert(GET(dog, Object, refs) == 1);
{
cpp::Dog* cppDog = ObjectProxy::of<cpp::Dog>(dog);
// Proxies don't increase the Object's reference count
assert(GET(dog, Object, refs) == 1);
cppDog->speak();
// Proxy is valid until Object is deleted.
}
assert(GET(dog, Object, refs) == 1);
Object_unref(dog);
return 0;
}