-
Notifications
You must be signed in to change notification settings - Fork 0
/
String.cpp
64 lines (49 loc) · 1.06 KB
/
String.cpp
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
#include <string>
#include <iostream>
class String
{
private:
char* m_data;
public:
String(const char* cstr=0);
String(const String& str);
String& operator=(const String& str);
~String();
char* get_c_str() const {return this->m_data;}
};
inline String::String(const char* cstr){
if (cstr){
m_data = new char[strlen(cstr) + 1];
strcpy(m_data, cstr);
}
else{
m_data = new char[1];
*m_data = '\0';
}
}
inline String::String(const String& str){
m_data = new char[strlen(str.m_data) + 1];
strcpy(m_data, str.m_data);
}
inline String::~String(){
delete[] m_data;
}
inline String& String::operator=(const String& str){
if (this == &str){
return *this;
}
delete[] m_data;
m_data = new char[strlen(str.m_data) + 1];
strcpy(m_data, str.m_data);
return *this;
}
std::ostream& operator<<(std::ostream& os, const String str){
os << str.get_c_str();
return os;
}
int main(){
char ptr[] = "Hello\0";
String c(ptr);
std::cout << c;
return 0;
}