-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_example.cpp
More file actions
41 lines (29 loc) · 993 Bytes
/
Copy pathclass_example.cpp
File metadata and controls
41 lines (29 loc) · 993 Bytes
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
#include <iostream>
#include <memory>
#include "delegate.hpp"
class Character {
public:
Character(std::string name) : name_(name) {}
void OnLevelUp(int newLevel) {
std::cout << "Character [" << name_ << "] is now level " << newLevel << "!" << std::endl;
}
private:
std::string name_;
};
int main() {
Delegate<void(int)> levelUpDelegate;
Character player1("Hero");
levelUpDelegate.add(&player1, &Character::OnLevelUp);
auto player2 = std::make_shared<Character>("Mage");
levelUpDelegate.add_weak(player2, &Character::OnLevelUp);
std::cout << "First Broadcast:" << std::endl;
levelUpDelegate.broadcast(10);
std::cout << "\nDestroying Mage (shared_ptr)..." << std::endl;
player2.reset();
std::cout << "Second Broadcast (only Hero should respond):" << std::endl;
levelUpDelegate.broadcast(11);
levelUpDelegate.remove_all_for_object(&player1);
std::cout << "\nFinal Broadcast (empty):" << std::endl;
levelUpDelegate.broadcast(12);
return 0;
}