Infinite Retrieval
Advanced Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору Вот задали поиграться с конструкторами копии, и не компилится мой код. Ошибку пометил в коментарии. Заранее спасибо. Код: #include <iostream> #include <string.h> #include <windows.h> class CMyString { char *pStr; protected: static int Q_OBJ; public: CMyString(); static char CON_C[100], DES[100], ADR[100], ADR_C[100], ADR_T[100], CON[100]; static int ShowNumberOfObj() { return Q_OBJ; } static void Qpp() { Q_OBJ++; } static void Qmm() { Q_OBJ--; } CMyString(const CMyString &); ~CMyString(); void Set(char *); }; int CMyString::Q_OBJ = 0; char CMyString::CON[100] = "The constructor of new object was called, now the number of objects is "; char CMyString::CON_C[100] = "The constructor of copy was called, now the number of objects is "; char CMyString::DES[100] = "The destructor was called, now the number of objects is "; char CMyString::ADR[100] = "The adress of new object is "; char CMyString::ADR_C[100] = "This constructor of copy was called by the object with the adress "; char CMyString::ADR_T[100] = "The adress of temprary object, that was created by the constructor of copy is "; CMyString::CMyString() { char buf[20]; pStr = new char[1]; Qpp(); strcat(CON, itoa(ShowNumberOfObj(), buf, 10)); MessageBoxA(NULL, CON, "Constructor", MB_OK); CON[72] = 0; //72 is lenth of CON } CMyString::~CMyString() { char buf[20]; Qmm(); strcat(DES, itoa(ShowNumberOfObj(), buf, 10)); MessageBoxA(NULL, DES, "Destructor", MB_OK); DES[57] = 0; //57 is lenth of DES delete pStr; } void CMyString::Set(char *pNewStr) { int i, s; s = strlen(pNewStr); delete pStr; pStr = new char[s+1]; for(i=0; i<=s; i++) *(pStr + i) = *(pNewStr + i); } CMyString::CMyString(const CMyString &Source) { char buf[20], q_str[12]; Qpp(); strcat(CON_C, itoa(ShowNumberOfObj(), buf, 10)); MessageBoxA(NULL, CON_C, "Constructor of copy", MB_OK); CON_C[65] = 0; //65 is lenth of CON strcat(ADR_C, itoa((int)(&Source), buf, 16)); MessageBoxA(NULL, ADR_C, "Constructor of copy", MB_OK); ADR_C[67] = 0; strcat(ADR_T, itoa((int)this, buf, 16)); MessageBoxA(NULL, ADR_T, "Constructor of copy", MB_OK); ADR_T[79] = 0; pStr = new[strlen(Source.pStr)]; // ОШИБКА!!!!!!!!!!!!!! ПОЧЕМУ? strcpy(pStr, Source.pStr); } void swap(int &x, int &y) { int temp = x; x = y; y = temp; } int main() { CMyString obj1; CMyString obj2 = obj1; return 0; } |
|