MCPcopy Create free account
hub / github.com/ShiqiYu/CPP / MyString

Class MyString

week11/examples/example2/mystring.hpp:5–66  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3#include <cstring>
4
5class MyString
6{
7 private:
8 int buf_len;
9 char * characters;
10 public:
11 MyString(int buf_len = 64, const char * data = NULL)
12 {
13 std::cout << "Constructor(int, char*)" << std::endl;
14 this->buf_len = 0;
15 this->characters = NULL;
16 create(buf_len, data);
17 }
18 MyString(const MyString & ms)
19 {
20 std::cout << "Constructor(MyString&)" << std::endl;
21 this->buf_len = 0;
22 this->characters = NULL;
23 create(ms.buf_len, ms.characters);
24 }
25 ~MyString()
26 {
27 release();
28 }
29 MyString & operator=(const MyString &ms)
30 {
31 create(ms.buf_len, ms.characters);
32 return *this;
33 }
34 bool create(int buf_len, const char * data)
35 {
36 release();
37
38 this->buf_len = buf_len;
39
40 if( this->buf_len != 0)
41 {
42 this->characters = new char[this->buf_len]{};
43 }
44 if(data)
45 strncpy(this->characters, data, this->buf_len);
46
47 return true;
48 }
49 bool release()
50 {
51 this->buf_len = 0;
52 if(this->characters!=NULL)
53 {
54 delete []this->characters;
55 this->characters = NULL;
56 }
57 return 0;
58 }
59 friend std::ostream & operator<<(std::ostream & os, const MyString & ms)
60 {
61 os << "buf_len = " << ms.buf_len;
62 os << ", characters = " << static_cast<void*>(ms.characters);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected