Allocating object arrays

Started by
0 comments, last by JaRoS 22 years, 5 months ago
It''s soo cool that you can allocate an entire array of pointer objects, but I have one question about the process. When you allocate just one object like this... balance *p; p = new balance; p->SomeFunctionIDidNotCreate(); delete p; return(true); it uses the arrow operator. However, if you allocate an array of objects like this... balance *p p = new balance [3]; p[0].SomeFunctionIDidNotCreate(); delete [] p; return(true); it uses the dot operator. I''m sure there is a valid reason, or is there? JaRoS
JaRoS
Advertisement
The variable "holding" an array _is_ a pointer. And conversely, a pointer is the base address of an array (with one element, usually, but C/C++ don''t do bounds checking)

In one case, you are accessing p, which is a pointer, in the second case, you are accessing p[0], which is the object pointed by p.

p[x] is equivalent to *(p+x)
so p[x].foo() is equivalent to (p+x)->foo()

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement