Solved

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

Posted on 2004-09-13
6
225 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
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now