-
Notifications
You must be signed in to change notification settings - Fork 1
/
stringcl.cpp
executable file
·69 lines (59 loc) · 1.38 KB
/
stringcl.cpp
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
67
68
69
#include"stringcl.hpp"
#include<stdio.h>
stringCls::stringCls()
{p =new srep ; p->s = 0 ;}
stringCls::stringCls(const stringCls & x)
{ x.p->n ++ ; p =x.p ; }
stringCls::stringCls(const char *s)
{
p= new srep ;
p-> s = new char[strlen(s) + 2] ;
strcpy(p->s , s ) ;
}
stringCls::stringCls( char *s)
{ p= new srep ; p-> s = new char[strlen(s) + 2] ; strcpy(p->s , s ) ;}
stringCls::~stringCls()
{
if( --p->n == 0){
delete[] p->s ;
delete p ;
}
}
stringCls & stringCls::operator=(const char *s )
{
if(p-> n > 1 ){
p-> n -- ;
p = new srep ;
}else
delete[] p->s ;
p->s = new char[strlen(s)+2] ;
strcpy(p->s , s ) ;
return *this ;
}
stringCls & stringCls::operator=(const stringCls &x )
{
x.p-> n ++ ; //protect against st = st
if(--p->n == 0)
{ delete[] p->s ; delete p ; }
p = x.p ;
return *this ;
}
ostream & operator<<(ostream & s , const stringCls & x )
{
return s << x.p->s ;// return s << x.p->s << "[ " << x.p->n << "]\n";
}
istream & operator>>(istream & s ,stringCls & x )
{
char buf[256] ;
gets(buf) ;
//s >> buf ;//unsafe might overflow
x = buf ; // overload =
// err_class_calc << "echo : " << x << '\n' ;
return s ;
}
/*
TOostream & operator<<(TOostream & s , const stringCls & x )
{
return s << x.p->s ;// return s << x.p->s << "[ " << x.p->n << "]\n";
}
*/