Solved

Help me... please... object array problems...

Posted on 2004-09-13
6
252 Views
Last Modified: 2010-04-01

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
0
Comment
Question by:bganoush
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12045782
You should bettr use a 'std::list', e.g. like

#include <list>
using namespace std;

list<CMyObject*> myList;

CMyObject * myObj = new CMyObject;

myList.push_back (myObj);

//...

list<CMyObject*>::iterator i = myList.begin();

CMyObject * object = *i;

0
 
LVL 1

Accepted Solution

by:
zekard earned 500 total points
ID: 12045836
How about this?

/// tested in VC++ 6.0

#include <iostream>

using namespace std;

class CMyObject
{
public:
      int x;
      int y;
      CMyObject (); // constructor
};

CMyObject::CMyObject ()
{
}

int main ()
{
      CMyObject *myObj = new CMyObject ();
      CMyObject ** objectPtrList = NULL;

      void * myList = NULL;
      void * myNewList = NULL;
      int numItems = 0;

      myNewList = realloc (myList, (numItems + 1) * sizeof (CMyObject *));
      if (myNewList != NULL)
      {      
            myList = myNewList;
            objectPtrList = (CMyObject **)myList;
            objectPtrList[numItems] = myObj;
            numItems ++;
      }

      return 0;

}
0
 

Expert Comment

by:kyleleber
ID: 12045852
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bganoush
ID: 12046190
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!
0
 
LVL 86

Expert Comment

by:jkr
ID: 12046240
>>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?
0
 

Author Comment

by:bganoush
ID: 12047467

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
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question