CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2002
    Posts
    3

    Angry member access question

    Thanks for any help on this:

    say i define a class which handles a list of structures

    class MyList{

    public:
    /*
    constructor creates array of ListItems along the lines of:
    pList = new ListItem[nElements];
    */
    MyList(UINT nElements);

    // subscript operator retruns ListItem reference
    ListItem &operator[](UINT pos);

    private:
    ListItem *pList;
    };


    struct ListItem {
    char strA[32];
    char strB[32];
    };



    i want to use this as follows:

    MyList *pList = new MyList(20);
    strcpy(pList[3].strA, "Hi There");

    the problem is that although operator[] returns a ListItem reference it is still a member of MyList, so pList[3].strA attempts to access a member of MyList and this will not compile. how do i get the functionality where the member access operator looks for a ListItem member, not a MyList member?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449
    Why not create a function that assigns to the ListItem instead of having the client do this? Proper C++ class design would hide these details from the client.
    Code:
    class MyList
    {
        public:
              void SetStrA(const char *pData,
                                   int whichone)
              { 
                   strcpy(pList[whichone], pData);
               }
      //...
    };
    
    //...
    MyList *pList = new MyList(20);
    pList[3].SetStrA("Hi There", 3);
    There is no check to see if "whichone" is in bounds, but this is the way it should have been coded.

    There are a lot of other issues with your class design that I won't comment on, such as the overall safety of the coding that you're doing and non-usage of the C++ standard library, but that is another thread.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jan 2001
    Posts
    253
    The pList variable is a pointer to a MyList. In order to access the operator[] on this variable, you need an object reference, not a pointer.

    So, you can either do this:

    MyList *pList = new MyList(20);
    strcpy( (*pList)[3].strA, "Hi There"); // dereference pList

    or you can declare a list object directly:

    MyList myList(20);
    strcpy( myList[3].strA, "Hi There" );


    If I were coding this, I would instead just use std::vector<ListItem> to hold the array of items.

    Best regards,
    John

  4. #4
    Join Date
    Mar 2002
    Location
    Israel
    Posts
    187
    Direct access to class members is not good (read ugly)
    coding practice.
    From every reason You want.
    The solution suggested by Paul is in any case preferable.

  5. #5
    Join Date
    Jan 2001
    Posts
    253
    I agree that direct access to members is ugly. However, Paul's solution has a minor bug (probably caused by cutting and pasting) - he left in the array access in the statement using the new member routine (accessing past the end of allocated memory).

    The correct call (using Paul's method) would be:

    pList->SetStrA( "Hi There", 3 );

    Best regards,
    John

  6. #6
    Join Date
    Jun 2002
    Posts
    137

    Exclamation Your code is not wrong

    You have to use your code like the following

    MyList *pml = new MyList(20);
    strcpy(pml[3][3].strA, "Hi There");
    delete pml;

    Maybe the variable name (MyList *) pList cause the misconception. pList in your code is a MyList pointer, the operator[] works on the class object not the class pointer of course, as what mentioned by jwbarton.
    Last edited by sandodo; June 20th, 2002 at 12:18 AM.

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

Featured