Allocating object arrays
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
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()
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()
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement