Solved

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

Posted on 2004-09-13
6
243 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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 …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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.

803 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