-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.h
More file actions
78 lines (70 loc) · 1.04 KB
/
String.h
File metadata and controls
78 lines (70 loc) · 1.04 KB
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
#define _CRT_SECURE_NO_DEPRECATE
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class String
{
public:
String(const char *str = NULL)
{
if (str == NULL)
{
_data = new char[1];
_data = '\0';
return;
}
size_t len = strlen(str) + 1;
_data = new char[len];
if (_data == NULL)
{
return;
}
strcpy(_data, str);
}
String(const String &other) //É±´
{
size_t len = strlen(other._data) + 1;
_data = new char[len];
if (_data != NULL)
{
strcpy(_data, other._data);
}
}
~String()
{
delete []_data;
}
String &operator=(const String &other)
{
if (this == &other)
{
return *this;
}
size_t len = strlen(other._data) + 1;
char *tmp = new char[len];
if (tmp != NULL)
{
delete[] _data;
strcpy(tmp, other._data);
_data = tmp;
}
return *this;
}
void Print()
{
cout << _data << endl;
}
protected:
char *_data;
};
void TestString()
{
String str("hello");
str.Print();
String str1(str);
str1.Print();
String src("world");
src = str = str1;
src.Print();
}