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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
#include <iostream>
#include <cstring>
//#define DISABLE_MOVE
#define FIX_BUG
using std::cout;
class CMessage
{
private: // Private members of the class
char* pmessage;
const char* howMade;
int instId;
int instFromId;
const char* instName;
static int nextInstId;
public: // Public interface of the class
// Public member functions
void ShowIt(void) const
{
cout << this->pmessage;
}
// Class (copy) constructors/destructors
explicit CMessage (const char* name, const char* text = "Default message.\n")
: pmessage(nullptr), howMade("init"), instId(nextInstId++), instFromId(-1), instName((name == nullptr) ? "null" : name)
{
cout << "Constructor called [" << instId << ", " << howMade
<< ", " << instFromId << ", " << instName << "]\n";
size_t length = strlen(text);
cout << "length #2 = " << length << "\n";
pmessage = new char[length + 1];
strcpy_s(pmessage, length + 1, text);
}
CMessage(const CMessage& aMessage)
: pmessage(nullptr), howMade("copy"), instId(nextInstId++), instFromId(aMessage.instId), instName("anon")
{
cout << "Copy constructor called [" << instId << ", " << howMade
<< ", " << instFromId << ", " << instName << "] from [" << aMessage.instId
<< ", " << aMessage.howMade << ", " << aMessage.instFromId << ", "
<< aMessage.instName << "]\n";
pmessage = new char[strlen(aMessage.pmessage) + 1];
strcpy_s(pmessage, strlen(aMessage.pmessage) + 1, aMessage.pmessage);
}
#ifndef DISABLE_MOVE
CMessage(CMessage&& aMessage)
: pmessage(nullptr), howMade("move"), instId(nextInstId++), instFromId(aMessage.instId), instName("anon")
{
cout << "Move copy constructor called [" << instId << ", " << howMade
<< ", " << instFromId << ", " << instName << "] from [" << aMessage.instId
<< ", " << aMessage.howMade << ", " << aMessage.instFromId << ", "
<< aMessage.instName << "]\n";
pmessage = aMessage.pmessage;
aMessage.pmessage = nullptr;
}
#endif
~CMessage() // Class destructor
{
cout << "Destructor called [" << instId << ", " << howMade
<< ", " << instFromId << ", " << instName << "]\n";
delete [] pmessage;
}
// Operator overloading
CMessage& operator = (const CMessage& aMessage)
{
cout << "Assignment operator overload called [" << instId << ", " << howMade
<< ", " << instFromId << ", " << instName << "] from [" << aMessage.instId
<< ", " << aMessage.howMade << ", " << aMessage.instFromId << ", "
<< aMessage.instName << "]\n";
if(this == &aMessage)
return *this;
if(this->pmessage != nullptr)
delete [] pmessage;
pmessage = new char[strlen(aMessage.pmessage) + 1];
strcpy_s(pmessage, strlen(aMessage.pmessage) + 1, aMessage.pmessage);
return *this;
}
#ifndef DISABLE_MOVE
CMessage& operator = (CMessage&& aMessage)
{
cout << "Move assignment operator overload called [" << instId << ", " << howMade
<< ", " << instFromId << ", " << instName << "] from [" << aMessage.instId
<< ", " << aMessage.howMade << ", " << aMessage.instFromId << ", "
<< aMessage.instName << "]\n";
delete [] pmessage;
pmessage = aMessage.pmessage;
aMessage.pmessage = nullptr;
return *this;
}
#endif
CMessage operator + (const CMessage& aMessage) const
{
cout << "Add operator overload called [" << instId << ", " << howMade
<< ", " << instFromId << ", " << instName << "] add [" << aMessage.instId
<< ", " << aMessage.howMade << ", " << aMessage.instFromId << ", "
<< aMessage.instName << "]\n";
size_t length = strlen(this->pmessage) + strlen(aMessage.pmessage) + 1;
cout << "length #1 = " << length << "\n";
#ifndef FIX_BUG
CMessage Message(new char[length]);
strcpy_s(Message.pmessage, length, this->pmessage);
strcat_s(Message.pmessage, length, aMessage.pmessage);
#else
char* psz = new char[length];
strcpy_s(psz, length, this->pmessage);
strcat_s(psz, length, aMessage.pmessage);
CMessage mottoTemp("mottoTemp", psz); // was Message
delete [] psz;
#endif
return mottoTemp;
}
};
/*static*/ int CMessage::nextInstId = 0;
int main(void)
{
// renamed from motto1, 2, 3 as numbers used for inst IDs
CMessage mottoA("mottoA", "I am Ozymandias, king of kings. ");
CMessage mottoB("mottoB", "Look on my works ye mighty and despair.\n");
CMessage mottoC("mottoC");
cout << "\nExecuting: mottoC = mottoA + mottoB.\n";
mottoC = mottoA + mottoB;
cout << "Done!\n\n";
mottoC.ShowIt();
cout << "\n";
return 0;
}
|