Код: Выделить всё
#include "stdafx.h"
#include "conio.h"
#include <string.h>
using namespace std;
int a;
class String {
private:
char* ptr;
char* ptr1;
public:
String(){
ptr = new char[1];
ptr[0] = '\0';
}
String(char* s){
int n = strlen(s);
ptr = new char[n + 1];
strcpy(ptr, s);
}
String(const String& src){
cout << "Vizivaetsja konstruction " << endl;
int n = strlen(src.ptr);
ptr = new char[n + 1];
strcpy(ptr, src.ptr);
}
~String(){
delete [] ptr;
}
String& operator=(const String& src){
cout << "Vizivaetsja operator prisvaivanija" << endl;
cout << "src.ptr = " << src.ptr << endl;
cout << "src.ptr1 = " << src.ptr1 << endl;
cpy(src.ptr);
return *this;
}
String operator+(char* s){
cout << "Vizivaetsja operator+" << endl;
String new_str(ptr);
cout << "S = " << s << endl;
cout << "new_str.ptr = " << new_str.ptr << endl;
cout << "ptr = " << ptr << endl;
new_str.cat(s);
return new_str;
}
operator char*() {
cout << "vizvalsja operator char*" << endl;
cout << "ptr = " << ptr << endl;
return ptr;
}
void cat(char* s){
int n = strlen(ptr) + strlen(s);
cout <<".......... " << ptr << endl;
cout <<".......... " << s << endl;
char* p1 = new char[n + 1];
strcpy(p1, ptr);
strcat(p1, s);
delete [] ptr;
ptr = p1;
}
void cpy(char* s){
delete [] ptr;
int n = strlen(s);
ptr = new char[n + 1];
strcpy(ptr, s);
}
};
int main() {
String a, b, c;
a = "I ";
b = "am ";
c = "so ";
String d = a + b + c;
cout << d;
cout << endl;
return 0;
}
Код: Выделить всё
a = "I ";
Код: Выделить всё
String operator=(const char*){
...
}
напр
Код: Выделить всё
String operator=(const char*){
...
}
ПОМОГИТЕ !
Просто в учебнике вообще по тупорылому все объясняется, не понятно и поверхностно