trouble with pointer to object

This is a discussion on trouble with pointer to object within the C++ Programming forums, part of the General Programming Boards category; Hi All, I have a class called CLine, and have declared a pointer by Code: CLine *pWhichGen; I then point ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    27

    trouble with pointer to object

    Hi All,
    I have a class called CLine, and have declared a pointer by
    Code:
    CLine *pWhichGen;
    I then point it to an object in an array by:
    Code:
    pWhichGen = &LineArray[0];
    I then want to be able to view the members of LineArray[0] through the pointer. One of these members is
    Code:
    int m_LineCrosses.
    When I then put in a statement like
    Code:
    int TwoLines = pWhichGen.m_LineCrosses
    I get the error
    Code:
    error C2228: left of '.m_LineCrosses' must have class/struct/union type
    Does anyone know how to do what i'm trying?
    Many thanks
    Dylan

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    int TwoLines = pWhichGen.m_LineCrosses

    Here I think you need:
    int TwoLines = pWhichGen->m_LineCrosses

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    27

    thanks

    thats the trick
    thank you very much

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The explanation is that pWhichGen is a pointer. Pointers do not have members--they are addresses--but the objects they point to do have members. To access the object pWhichGen points to, you would refer to it as *pWhichGen and to access the members of *pWhichGen you would need to do this:

    (*pWhichGen).m_LineCrosses

    but that's cumbersome notation, so you're able to use this notation instead:

    pWhichGen->m_LineCrosses.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  3. How would you do this (object interlinking)
    By darksaidin in forum C++ Programming
    Replies: 7
    Last Post: 08-30-2003, 12:08 AM
  4. Testing object pointer for null
    By VanJay011379 in forum C++ Programming
    Replies: 4
    Last Post: 07-24-2002, 10:51 AM
  5. Void pointer. So, is this an actual object?
    By Boksha in forum C++ Programming
    Replies: 5
    Last Post: 06-29-2002, 05:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21