Re: C++ Syntax Confusion
On May 20, 6:46 am, coder_...@yahoo.com wrote:
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 ?
It's a pointer to a single array which can containt 10 elements,
yes.
(*iarrayPtr)[1] = 99;
Correct. Dereferencing the pointer results in the array itself.
2. Assign value 99 to array element 1.
iarrayPtr[2] = 100;
This is undefined behavior. For historical reasons, [] is
defined on pointers, and is the exactly equivalent (in this
case) of *(iarrayPtr + 2). In short, you are assigning 100 to
the third Array object allocated by new. Which is wrong for
several reasons: first, you didn't allocate three objects, only
one, so what you get it undefined behavior, and second, you
haven't defined an assignment operator for Array objects, so the
compiler generated one will be used, and I'm 100% certain it
doesn't do what you want (since it does a shallow copy).
Finally, of course, you're assigning an int, and not an Array
object, so in the absence of an explicit "operator=(int)" in
your Array class, the compiler will try to convert the int to an
Array object.
3. Stepping through with the debugger, I can see Array(int32 aSize)
called with aSize set to 100. What happened here?
The compiler is doing what you told it to do, not what you
actually want:-). See my answer to 2. Also, you "told" the
compiler that it could implicitly convert an int to an Array
object.
=== ===
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[i]);}
T& operator[](int32 i){return(data[i]);}
T *getData() const {return(data);}
private:
Array();
private:
T *data;
int32 dataLen;
};
Given this:
-- you definitly want to declare the constructor explicit, and
-- you either want to define a copy constructor and assignment
operator with the semantics you want, or declare them
private, so that the compiler can't use them just anywhere.
Neither of these corrections, however, will allow you to refer
the the third Array object when you've only allocated one.
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34