Click to See Complete Forum and Search --> : member access question


mandrews58
June 19th, 2002, 12:11 PM
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?

Paul McKenzie
June 19th, 2002, 12:42 PM
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.

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

jwbarton
June 19th, 2002, 12:50 PM
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

Alexis Moshinsky
June 19th, 2002, 01:06 PM
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.

jwbarton
June 19th, 2002, 03:55 PM
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

sandodo
June 20th, 2002, 12:04 AM
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.