]>
Commit | Line | Data |
---|---|---|
1 | #include "myString.h" | |
2 | #include <iostream> | |
3 | using std::cout; | |
4 | using std::endl; | |
5 | ||
6 | myString::myString() | |
7 | { | |
8 | string = NULL; | |
9 | } | |
10 | ||
11 | myString::myString(char *s) | |
12 | { | |
13 | setString(s); | |
14 | } | |
15 | ||
16 | myString::myString(const myString &right) | |
17 | { | |
18 | setString(&right); | |
19 | } | |
20 | ||
21 | myString::~myString() | |
22 | { | |
23 | if (string) | |
24 | delete [] string; | |
25 | ||
26 | string = NULL; | |
27 | } | |
28 | ||
29 | void myString::setString(char *s) | |
30 | { | |
31 | if (!s) | |
32 | { | |
33 | if (string) | |
34 | delete [] string; | |
35 | ||
36 | string = NULL; | |
37 | } | |
38 | else | |
39 | { | |
40 | delete []string; | |
41 | string = new char[strlen(s) + 1]; | |
42 | memset(string, 0, (strlen(s) + 1)); | |
43 | strcpy(string, s); | |
44 | } | |
45 | } | |
46 | void myString::setString(const char *s) | |
47 | { | |
48 | if (!s) | |
49 | { | |
50 | if (string) | |
51 | delete [] string; | |
52 | ||
53 | string = NULL; | |
54 | } | |
55 | else | |
56 | { | |
57 | delete []string; | |
58 | string = new char[strlen(s) + 1]; | |
59 | memset(string, 0, (strlen(s) + 1)); | |
60 | strcpy(string, s); | |
61 | } | |
62 | } | |
63 | ||
64 | void myString::setString(const myString *right) | |
65 | { | |
66 | setString(right->string); | |
67 | } | |
68 | ||
69 | myString &myString::operator=(myString &right) | |
70 | { | |
71 | setString(right.string); | |
72 | return *this; | |
73 | } | |
74 | ||
75 | myString &myString::operator=(char *right) | |
76 | { | |
77 | setString(right); | |
78 | return *this; | |
79 | } | |
80 | ||
81 | myString &myString::operator=(const char *right) | |
82 | { | |
83 | setString(right); | |
84 | return *this; | |
85 | } |