String类简单了解

 

 

 1 #include <stdio.h>
2 #include <string.h>
3
4 class String
5 {
6 public:
7 String(const char *str = NULL);
8 ~String();
9 String(const String &other);
10 String& operator = (const String &other);
11 private:
12 char *m_data;
13 };
14
15 String::~String()
16 {
17 delete [] m_data;
18 }
19
20 String::String(const char *str)
21 {
22 if (!str)
23 {
24 m_data = new char[1];
25 m_data[0] = '\0';
26 }
27 else
28 {
29 int len = strlen(str);
30 m_data = new char[len + 1];
31 strcpy(m_data, str);
32 }
33 }
34
35 String::String(const String& other)
36 {
37 /*
38 if (!other)
39 {
40 m_data = new char[1];
41 m_data = '\0';
42 }
43 else
44 {
45 */
46 delete m_data;
47 int len = strlen(other.m_data);
48 m_data = new char[len + 1];
49 strcpy(m_data, other.m_data);
50
51 //}
52 }
53
54 String & String::operator=(const String& other)
55 {
56 if (this == &other)
57 {
58 return *this;
59 }
60 else
61 {
62 delete m_data;
63 int len = strlen(other.m_data);
64 m_data = new char[len + 1];
65 strcpy(m_data, other.m_data);
66 return *this;
67 }
68 }
69
70
71 int main()
72 {
73
74 return 0;
75 }

 

转载于:https://www.cnblogs.com/Clin/archive/2011/11/09/2242221.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值