C
coder_lol
Given the template class below, please help me understand the
following code behavior?
Array<int32> ia(10); // Array that can contain 10 int32
ia[1] = 1; // Array element 1 set to 1
Here comes the confusion part.
Array<int32> *iarrayPtr = new Array<int32>(10);
1. Does the above mean a point to an array that can contain 10
int32 ?
(*iarrayPtr)[1] = 99;
2. Assign value 99 to array element 1.
iarrayPtr[2] = 100;
3. Stepping through with the debugger, I can see Array(int32 aSize)
called with aSize set to 100. What happened here? How should I read
the above code statement?
Thanks!!!
=== ===
template<class T>
class Array :{
public:
Array(int32 aSize){dataLen = aSize;data = new T[dataLen];}
virtual ~Array(){delete [] data;}
int32 length() const{return(dataLen);}
const T& operator[](int32 i) const {return(data);}
T& operator[](int32 i){return(data);}
T *getData() const {return(data);}
private:
Array();
private:
T *data;
int32 dataLen;
};
following code behavior?
Array<int32> ia(10); // Array that can contain 10 int32
ia[1] = 1; // Array element 1 set to 1
Here comes the confusion part.
Array<int32> *iarrayPtr = new Array<int32>(10);
1. Does the above mean a point to an array that can contain 10
int32 ?
(*iarrayPtr)[1] = 99;
2. Assign value 99 to array element 1.
iarrayPtr[2] = 100;
3. Stepping through with the debugger, I can see Array(int32 aSize)
called with aSize set to 100. What happened here? How should I read
the above code statement?
Thanks!!!
=== ===
template<class T>
class Array :{
public:
Array(int32 aSize){dataLen = aSize;data = new T[dataLen];}
virtual ~Array(){delete [] data;}
int32 length() const{return(dataLen);}
const T& operator[](int32 i) const {return(data);}
T& operator[](int32 i){return(data);}
T *getData() const {return(data);}
private:
Array();
private:
T *data;
int32 dataLen;
};