File tree 3 files changed +44
-1
lines changed
SampleProjects/TestSomething/test
3 files changed +44
-1
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
19
19
- Replace ` #define yield() _NOP() ` with ` inline void yield() { _NOP(); } ` so that other code can define a ` yield() ` function.
20
20
- Update .gitattributes so we have consistent line endings
21
21
- Run tests on push as well as on a pull request so developers can see impact
22
+ - Apply "rule of three" to Client copy constructor and copy assignment operator
22
23
23
24
### Deprecated
24
25
Original file line number Diff line number Diff line change @@ -19,6 +19,29 @@ unittest(Client) {
19
19
assertEqual (outData + " \r\n " , inData);
20
20
}
21
21
22
+ unittest (Client_copy_constructor) {
23
+ { // Client object contains a reference to a String object
24
+ Client c1; // Constructor instantiates a String (string1)
25
+ Client c2; // Constructor instantiates a String (string2)
26
+ c1.write (' 1' );
27
+ c2.write (' 2' );
28
+ assertEqual (" 1" , *(c1.mGodmodeDataIn ));
29
+ assertEqual (" 2" , *(c2.mGodmodeDataIn ));
30
+ c2 = c1; // c2 should get a copy of s1, not a reference to it
31
+ // and string2 should have been deleted during the assignment
32
+ assertNotEqual (c1.mGodmodeDataIn , c2.mGodmodeDataIn );
33
+ assertEqual (" 1" , *(c1.mGodmodeDataIn ));
34
+ assertEqual (" 1" , *(c2.mGodmodeDataIn ));
35
+ c1.write (' 1' );
36
+ c2.write (' 2' );
37
+ assertEqual (" 11" , *(c1.mGodmodeDataIn ));
38
+ assertEqual (" 12" , *(c2.mGodmodeDataIn ));
39
+ } // End of scope calls destructor on c1 and c2
40
+ // Memory monitoring will give an error if delete is called twice on string1
41
+ // The following assertion is just to confirm that we got through the above
42
+ assertTrue (true );
43
+ }
44
+
22
45
unittest (IPAddress) {
23
46
IPAddress ipAddress0;
24
47
assertEqual (0 , ipAddress0.asWord ());
Original file line number Diff line number Diff line change 1
1
#pragma once
2
2
3
- #include < Stream.h>
4
3
#include < IPAddress.h>
4
+ #include < Stream.h>
5
5
6
6
class Client : public Stream {
7
7
public:
@@ -11,6 +11,25 @@ class Client : public Stream {
11
11
mGodmodeDataIn = new String;
12
12
}
13
13
}
14
+ Client (const Client &client) { // copy constructor
15
+ if (this != &client) { // not a self-assignment
16
+ if (mGodmodeDataIn &&
17
+ client.mGodmodeDataIn ) { // replace what we previously had
18
+ delete mGodmodeDataIn ; // get rid of previous value
19
+ mGodmodeDataIn = new String (client.mGodmodeDataIn ->c_str ());
20
+ }
21
+ }
22
+ }
23
+ Client &operator =(const Client &client) { // copy assignment operator
24
+ if (this != &client) { // not a self-assignment
25
+ if (mGodmodeDataIn &&
26
+ client.mGodmodeDataIn ) { // replace what we previously had
27
+ delete mGodmodeDataIn ; // get rid of previous value
28
+ mGodmodeDataIn = new String (client.mGodmodeDataIn ->c_str ());
29
+ }
30
+ }
31
+ return *this ;
32
+ }
14
33
~Client () {
15
34
if (mGodmodeDataIn ) {
16
35
delete mGodmodeDataIn ;
You can’t perform that action at this time.
0 commit comments