Skip to content

Commit afaddb5

Browse files
committed
Add copy/move semantics
1 parent 95bc109 commit afaddb5

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

Formatter.hpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,25 @@ class Formatter {
2020
vsnprintf(_msg, MSG_LEN, fmt, args);
2121
va_end(args);
2222
}
23-
~Formatter() {
23+
Formatter(const Formatter &f) : _msg(new char[MSG_LEN]) { strcpy(_msg, f._msg); }
24+
Formatter(Formatter &&f) : _msg(f._msg) { f._msg = nullptr; }
25+
Formatter& operator=(const Formatter &f) {
26+
// copy assign
27+
if (this == &f) return *this;
2428
delete[] _msg;
25-
_msg = nullptr;
29+
_msg = new char[MSG_LEN];
30+
strcpy(_msg, f._msg);
31+
return *this;
2632
}
33+
Formatter& operator=(Formatter &&f) {
34+
// move assign
35+
if (this == &f) return *this;
36+
delete[] _msg;
37+
_msg = f._msg;
38+
f._msg = nullptr;
39+
return *this;
40+
}
41+
virtual ~Formatter() { delete[] _msg; _msg = nullptr; }
2742
const char* msg() const noexcept { return _msg; }
2843
};
2944

0 commit comments

Comments
 (0)