bganoush
asked on
Help me... please... object array problems...
Hi, I need to make an array of object pointers which is probably one of the easiest things to implement but I can't get my brain to wrap around this...
I create an object like this:
CMyObject * myObj = new CMyObject;
then I allocate some space in an array which is defined as a "void *", cast it to an array of pointers (CMyObject*)? and then assign the pointer to the new object to the end of the list:
declared as --> void * myList = NULL;
myNewList = realloc (myList, (numItems + 1) * sizeof (CMyObject *));
if (myNewList != NULL)
{
myList = myNewList;
CMyObject * objectPtrList = (CMyObject *)myList;
objectPtrList[numItems] = myObj;
numItems ++;
}
Now, I'm kind of constrained as this code needs to adhere to the "void *" for storing the array and the memory must be allocated with malloc/realloc except for objects such as CMyObject...
The compiler I use complains at the assignment of objectPtrList[numItems]...
-- Bubba
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It looks like your assignment:
objectPtrList[numItems] = myObj;
is trying to assign a pointer to an object.
myObj is type (CMyObject *)
objectPtrList expects type (CMyObject).
Try dereferencing myObj in the assignment:
objectPtrList[numItems] = *myObj; //If this is what you are trying to do
objectPtrList[numItems] = myObj;
is trying to assign a pointer to an object.
myObj is type (CMyObject *)
objectPtrList expects type (CMyObject).
Try dereferencing myObj in the assignment:
objectPtrList[numItems] = *myObj; //If this is what you are trying to do
ASKER
Zekard,
>> objectPtrList = (CMyObject **)myList;
>> objectPtrList[numItems] = myObj;
Oops! I knew that!
What I tried before was something like this:
CMyObject ** objectPtrList = (CMyObject *)myList;
objectPtrList[numItems] = myObj;
I forgot to cast "myList" to a pointer to pointers...
Thanks!
>> objectPtrList = (CMyObject **)myList;
>> objectPtrList[numItems] = myObj;
Oops! I knew that!
What I tried before was something like this:
CMyObject ** objectPtrList = (CMyObject *)myList;
objectPtrList[numItems] = myObj;
I forgot to cast "myList" to a pointer to pointers...
Thanks!
>>I forgot to cast "myList" to a pointer to pointers...
1st of all, your list is not a list, but an array of pointers. 2nd, why would ypu want to reinvent the wheel?
1st of all, your list is not a list, but an array of pointers. 2nd, why would ypu want to reinvent the wheel?
ASKER
1st:
English is not my first language, "list" sounds much closer to what it is called in my language. Thank you for correcting my unsuitably bad translation...
2nd:
It is what is expected of me... The management I am working with requires me to send an array of pointers to their precious engine. ie: red tape
But thank you so very much for caring!
-- Bubba
#include <list>
using namespace std;
list<CMyObject*> myList;
CMyObject * myObj = new CMyObject;
myList.push_back (myObj);
//...
list<CMyObject*>::iterator
CMyObject * object = *i;