-
the dot and ->
I have an array of objects on the free store
Obj *myObj = new Obj[10];
when i want to access these (compiler:MSVC++), i have to say myObj[x].whatever
but if i do just one object on the free store:
Obj *myObj = new Obj;
i must say myObj->whatever
If they are both pointers, why do i have to use the dot for one and the -> for the other?
-
I don't know if this contains the answer, but a question about these too was asked not too long ago:
http://www.vbforums.com/showthread.p...hreadid=215027
-
You could rewrite obj->member to (*obj).member. From this, you realize that obj[x].member is a form of dereferencing.
Z.
-
If you have a pointer:
Obj *ptr;
then
ptr[x]
is equivalent to writing
*(ptr+x)
So
ptr[x].elem
is equivalent to
(*(ptr+x)).elem
ptr->elem
is equivalent to
(*ptr).elem
so the only real difference is the offset x