-
Notifications
You must be signed in to change notification settings - Fork 323
/
mystring.hpp
66 lines (62 loc) · 1.49 KB
/
mystring.hpp
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
65
66
#pragma once
#include <iostream>
#include <cstring>
class MyString
{
private:
int buf_len;
char * characters;
public:
MyString(int buf_len = 64, const char * data = NULL)
{
std::cout << "Constructor(int, char*)" << std::endl;
this->buf_len = 0;
this->characters = NULL;
create(buf_len, data);
}
MyString(const MyString & ms)
{
std::cout << "Constructor(MyString&)" << std::endl;
this->buf_len = 0;
this->characters = NULL;
create(ms.buf_len, ms.characters);
}
~MyString()
{
release();
}
MyString & operator=(const MyString &ms)
{
create(ms.buf_len, ms.characters);
return *this;
}
bool create(int buf_len, const char * data)
{
release();
this->buf_len = buf_len;
if( this->buf_len != 0)
{
this->characters = new char[this->buf_len]{};
}
if(data)
strncpy(this->characters, data, this->buf_len);
return true;
}
bool release()
{
this->buf_len = 0;
if(this->characters!=NULL)
{
delete []this->characters;
this->characters = NULL;
}
return 0;
}
friend std::ostream & operator<<(std::ostream & os, const MyString & ms)
{
os << "buf_len = " << ms.buf_len;
os << ", characters = " << static_cast<void*>(ms.characters);
os << " [" << ms.characters << "]";
return os;
}
};