Skip to content

Commit f8ab8b8

Browse files
author
James Foster
committed
Rule of three: if you have a custom destructor, then you probably need a custom copy constructor and a custom copy assignment operator.
1 parent 84d7ad4 commit f8ab8b8

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

SampleProjects/TestSomething/test/clientServer.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ unittest(Client) {
1919
assertEqual(outData + "\r\n", inData);
2020
}
2121

22+
unittest(Client_copy_constructor) {
23+
Client client1;
24+
Client client2;
25+
client2 = client1;
26+
assertTrue(true);
27+
}
28+
2229
unittest(IPAddress) {
2330
IPAddress ipAddress0;
2431
assertEqual(0, ipAddress0.asWord());

cpp/arduino/Client.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ class Client : public Stream {
1111
mGodmodeDataIn = new String;
1212
}
1313
}
14+
Client(const Client &client) {
15+
// copy constructor
16+
if (mGodmodeDataIn) {
17+
mGodmodeDataIn = new String(mGodmodeDataIn->c_str());
18+
}
19+
std::cout << __FILE__ << ":" << __LINE__ << std::endl;
20+
}
21+
Client & operator=(const Client &client) {
22+
// copy assignment operator
23+
if (mGodmodeDataIn) {
24+
mGodmodeDataIn = new String(mGodmodeDataIn->c_str());
25+
}
26+
return *this;
27+
}
1428
~Client() {
1529
if (mGodmodeDataIn) {
1630
delete mGodmodeDataIn;

0 commit comments

Comments
 (0)