File tree Expand file tree Collapse file tree 3 files changed +44
-1
lines changed
SampleProjects/TestSomething/test Expand file tree Collapse file tree 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/).
1919- Replace ` #define yield() _NOP() ` with ` inline void yield() { _NOP(); } ` so that other code can define a ` yield() ` function.
2020- Update .gitattributes so we have consistent line endings
2121- 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
2223
2324### Deprecated
2425
Original file line number Diff line number Diff line change @@ -19,6 +19,29 @@ unittest(Client) {
1919 assertEqual (outData + " \r\n " , inData);
2020}
2121
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+
2245unittest (IPAddress) {
2346 IPAddress ipAddress0;
2447 assertEqual (0 , ipAddress0.asWord ());
Original file line number Diff line number Diff line change 11#pragma once
22
3- #include < Stream.h>
43#include < IPAddress.h>
4+ #include < Stream.h>
55
66class Client : public Stream {
77public:
@@ -11,6 +11,25 @@ class Client : public Stream {
1111 mGodmodeDataIn = new String;
1212 }
1313 }
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+ }
1433 ~Client () {
1534 if (mGodmodeDataIn ) {
1635 delete mGodmodeDataIn ;
You can’t perform that action at this time.
0 commit comments