FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
9E/12.10 ArraySubscriptOperator_MyString.cpp at main · learncppnow/9E · GitHub
learncppnow
/
9E
Public
Notifications
You must be signed in to change notification settings
Fork
32
Star
78
Code
Issues
0
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
9E
/
12.10 ArraySubscriptOperator_MyString.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
83 lines (69 loc) · 1.84 KB
Breadcrumbs
9E
/
12.10 ArraySubscriptOperator_MyString.cpp
Copy path
File metadata and controls
83 lines (69 loc) · 1.84 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#
include
<
iostream
>
#
include
<
algorithm
>
using
namespace
std
;
class
MyBuffer
{
private:
int
* myNums;
unsigned
int
bufLength;
public:
MyBuffer
(
unsigned
int
length)
{
bufLength = length;
myNums =
new
int
[length];
//
allocate memory
}
int
&
operator
[] (
unsigned
int
index)
{
return
myNums[index];
}
//
const version will be used by const instances of MyBuffer
const
int
&
operator
[] (
unsigned
int
index)
const
{
return
myNums[index];
}
MyBuffer
(
const
MyBuffer& src)
{
cout <<
"
Copy constructor creating deep copy
"
<< endl;
bufLength = src.
bufLength
;
myNums =
new
int
[bufLength];
copy
(src.
myNums
, src.
myNums
+ bufLength, myNums);
//
deep copy
}
MyBuffer&
operator
= (
const
MyBuffer& src)
{
cout <<
"
Copy Assignment creating deep copy
"
<< endl;
if
(myNums != src.
myNums
)
//
avoid copy to self
{
if
(myNums)
delete
myNums;
bufLength = src.
bufLength
;
myNums =
new
int
[bufLength];
copy
(src.
myNums
, src.
myNums
+ bufLength, myNums);
//
deep copy
}
return
*
this
;
}
~MyBuffer
()
{
delete[]
myNums;
//
free allocated memory
}
void
SetValue
(
unsigned
int
index,
int
value)
{
if
(index < bufLength)
//
check for bounds
*(myNums + index) = value;
}
};
int
main
()
{
cout <<
"
How many integers would you like to store?
"
;
unsigned
int
numsToStore =
0
;
cin >> numsToStore;
MyBuffer
buf
(numsToStore);
for
(
unsigned
int
counter =
0
; counter < numsToStore; ++counter)
{
cout <<
"
Enter value:
"
;
cin >> buf[counter];
}
for
(
unsigned
int
counter =
0
; counter < numsToStore; ++counter)
cout <<
"
Value
"
<< counter <<
"
is
"
<< buf[counter] << endl;
//
const MyBuffer bufConst(1); // requires const variant of operator[]
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL