-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo.hpp
More file actions
56 lines (47 loc) · 1.44 KB
/
demo.hpp
File metadata and controls
56 lines (47 loc) · 1.44 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
#pragma once
#include <list>
#include <string>
#include <memory>
namespace demo {
using std::list;
using std::string;
using std::unique_ptr;
//--------------------------------------------------------------------------
template<class M>
class member // e.g. node
{
public:
typedef list<M> children_t;
typedef typename children_t::iterator child_iterator;
member(M* parent)
: parent_(parent) {}
M* parent() { return parent_; }
child_iterator begin() { return children_.begin(); }
child_iterator end() { return children_.end(); }
template<class ...Args>
M& add_child(Args ...args) {
children_.push_back(M(static_cast<M*>(this),args...));
return children_.back();
}
private:
M* parent_;
children_t children_;
};
//--------------------------------------------------------------------------
class person: public member<person>
{
public:
person(person* parent, string const& name)
: member<person>(parent), name_(name) {}
string const& name() { return name_; }
private:
string name_;
};
//--------------------------------------------------------------------------
bool test_demo() {
person phil(nullptr, "Phil");
person& ben = phil.add_child("Ben");
//person& alek = phil.add_child("Alek");
return true;
}
}