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

Class MyString

week11/examples/example4/mystring.hpp:5–56  ·  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 &) = delete;
19 MyString & operator=(const MyString &) = delete;
20 ~MyString()
21 {
22 release();
23 }
24 bool create(int buf_len, const char * data)
25 {
26 release();
27
28 this->buf_len = buf_len;
29
30 if( this->buf_len != 0)
31 {
32 this->characters = new char[this->buf_len]{};
33 }
34 if(data)
35 strncpy(this->characters, data, this->buf_len);
36
37 return true;
38 }
39 bool release()
40 {
41 this->buf_len = 0;
42 if(this->characters!=NULL)
43 {
44 delete []this->characters;
45 this->characters = NULL;
46 }
47 return 0;
48 }
49 friend std::ostream & operator<<(std::ostream & os, const MyString & ms)
50 {
51 os << "buf_len = " << ms.buf_len;
52 os << ", characters = " << static_cast<void*>(ms.characters);
53 os << " [" << ms.characters << "]";
54 return os;
55 }
56};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected