consider class a:
class { public: int x; int y; }
now if write a *a = new a()
a pointer element of class a. however, if write a *a = new a[5]
a[0] not pointer instance of class.
i expecting a[n]
behave pointer , operations a[0]->x
hold valid.
what flaw in understanding?
when a
pointer, syntax a[n]
is, definition, equivalent (*(a + n))
. dereference included in expression. difference between new a()
, new a[n]
in what's allocated (a single element vs. multiple elements). however, in both cases, pointer single element returned (the element in case of new a()
, , first element in case of new a[n]
). , syntax can used on pointer same in both cases. note following:
a* = new a(); a[0].x = 10;
this legal (albeit uncommon) way access single object allocated expression new a()
. likewise:
a* = new a[n]; a->x = 10;
that legal way access first element of array allocated expression new a[n]
.