Solved

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

Posted on 2004-09-13
6
249 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

829 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