|
-
June 19th, 2002, 12:11 PM
#1
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?
-
June 19th, 2002, 12:42 PM
#2
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
-
June 19th, 2002, 12:50 PM
#3
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
-
June 19th, 2002, 01:06 PM
#4
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.
-
June 19th, 2002, 03:55 PM
#5
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
-
June 20th, 2002, 12:04 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|