-
Pointer question
Hey all,
If I have a pointer:
SomeObject *pointer;
and I want to access a method from that objects class, I would do it via pointer->someMethod.
However if I say that pointer = new SomeObject[numelements]; then when I want to do pointer[index]->someMethod it fails but pointer[index].someMethod works. I'm new to C++ so I'm not sure why this is the case.
Thanks
-
[] dereferences (did I spell that right?). That means [] takes pointers to what-they-point-to.
ptr+1 is a pointer.
*(ptr+1) is some type.
ptr[1] is the same as *(ptr+1).
we use -> for pointers and . for regular stuff.
-
pointer[index]->someMethod is equal to *(pointer[index]).someMethod but since pointer[index] is an array you dont need the "*" sign.
If it wasnt an array like *(pointer).someMethod, this pointer->someMethod would work.
An array is already like a pointer thats why you dont need the dereferencing sign.