-
Notifications
You must be signed in to change notification settings - Fork 2
/
Person.hpp
49 lines (36 loc) · 1.17 KB
/
Person.hpp
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
#ifndef EXAMPLE_HPP
#define EXAMPLE_HPP
#include <string>
#include <string_view>
#include <iostream>
namespace Test {
class Person {
public:
Person(const std::string& name) noexcept;
Person(const Person& other);
std::string getName() const;
bool setName(const std::string& t_newName);
private:
std::string m_name;
};
std::ostream& operator<<(std::ostream&, const Test::Person&);
// A free-standing function taking an object as argument
std::string personName(const Person& person);
inline void setName(Person &p, const std::string &newname) {
p.setName(newname);
}
// get an integral representation of the pointer that is this Person
inline long long toInt(Person &p) {
std::clog << "original pointer: " << &p << '\n';
const auto result = reinterpret_cast<long long>(&p);
std::clog << "toInt from C++ " << result << '\n';
return result;
}
// take the integer from toInt and reinterpret_cast it back into a Person *, then return that as a reference
inline Person &fromInt(long long i) {
auto *ptr = reinterpret_cast<Person *>(i);
std::clog << "Reclaimed pointer: " << ptr << '\n';
return *ptr;
}
} // Namespace Test
#endif // ifndef PERSON_HPP