Results 1 to 4 of 4

Thread: the dot and ->

  1. #1

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238

    Question 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?

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    You could rewrite obj->member to (*obj).member. From this, you realize that obj[x].member is a form of dereferencing.

    Z.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width