Second Order configured operator [] overloaded

_INTARRAY_H_ #ifndef
#define _INTARRAY_H_
class IntArray {
Private:
int m_length;
int * m_pointer;
IntArray (int len);
IntArray (const IntArray & obj);
BOOL Construct (); // effect: for memory array, initial values
public:
static IntArray * NewInstance (int length); // create the object function, plus the role of static? ?
BOOL GET (int index, int & value);
BOOL SET (int index, int value);
int & operator [] (int index); // operator overloading function
int length (); // always forget to get the array length as a function
IntArray & self (); // member function itself returns
~ IntArray ();
};
#endif

//IntArray.cpp

#Include. 1 "IntArray.h"
2 :: IntArray IntArray (int len) {
. 3 m_length = len; // constructor given by an array of length
4}
5 // array initial value and space applications, like in this function the benefits of a private member; a second stage second order constructor - application resources
. 6 BOOL IntArray :: Construct () {
. 7 RET = BOOL to true;
. 8 m_pointer = new new int [m_length];
. 9 IF (m_pointer) {
10 for (int I = 0; I <m_length; I ++) {
. 11 m_pointer [I] = I;
12 is}
13 is} the else {
14 RET = to false;
15}
16 return RET;
. 17}
18 is int IntArray :: length () {
. 19 m_length return;
20 is}
21 is IntArray :: * IntArray NewInstance, (int length) {
22 is IntArray new new IntArray * RET = (length);
(! (RET RET-&&> Construct ())) {IF 23 is
24 Delete RET;
25 ret = 0; // ?????? Why have to be 0, NULL is not?
} 26 is
27 return RET;
28}
29 BOOL IntArray :: GET (int index, int & value) {

30 // should note array subscript index and cross-border issues related to the determined index
31 is BOOL RET = (0 <= index && index <length ());
32 IF (RET) {
33 is m_pointer value = [index];
} 34 is
35 return RET;
36}
37 [BOOL IntArray :: SET (int index, int value) {
38 is BOOL RET = ((0 <= index) && (index <length ()));
39 IF (RET) {
40 m_pointer [index] = value;
41 is}
42 is return RET;
43 is}
44 is int & IntArray :: operator [] (int index) {
45 return m_pointer [index];
46 is}
47 IntArray & IntArray :: Self () {
48 return * the this ; // returns current array objects
49}
50 IntArray :: ~ IntArray () {
51 is Delete [] m_pointer;
52 is}

 

//main.cpp

#include <the iostream>
#include <String>
#include "IntArray.h"
#include <stdio.h>
the using namespace STD;
int main () {
// IntArray A [. 5]; X-
IntArray IntArray :: = A * NewInstance, (. 5);
IF (! a = NULL) {
// the stack to the subject a spatial aliases directed to avoid the use of pointers in C ++, second ah ~
IntArray & Array = A-> Self ();
COUT << be array.length () << endl;
for (int I = 0; I <be array.length (); I ++) {
COUT << array [I] << endl;
}
} // NOTE scope range target array, silly x! !
A Delete;
return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/DXGG-Bond/p/11871264.html