uhm.....dynamic arrays of pointers........


New member

I need to create an array of pointers to objects in C++

like so:

anObject *ptr;
ptr = new anObject[5];

but it needs to be an array of pointers to that object, not an array of that object.
I tried :

(note: col is an arbitrary number)

Node **ptrArrayAcross;

ptrArrayAcross = new int[cols];

for( int i = 0; i < cols; i++ ){

Node newNode = new Node( -1, i, 0, 0);
ptrArrayAcross = newNode;

tons of errors
Try this:

class c {};
int i;

c **c_arr = new c*[5];
for (i = 0; i < 5; i++)
    c_arr[i] =  new class c;

should work; g++ does not complain. Note the c* !
You guys need to get more object oriented than that! Where's the clist class?
clist? what?

This is my first class using C++ :bleh:

Just busting your balls.... When you get further along, hopefully they will teach you some object oriented concepts. The idea is to hide your implementation behind a class interface. This is called encapsulation. You can create a new clist class which contains a list of class c objects. (Using the names in you example not the MFC classes). Internally the clist class can save your objects in a dynamic array of pointers as you have in your code. But, you can hide this implementation behind a class interface. For example you can have an Add method that has a single parameter of a pointer to an object of Class c. You could also have a Count property that returns the number of objects stored, an Item method that returns a pointer to a single c Class based on a Index parameter, ...

The advantages to this method is that everywhere in your application you make calls to the clist class to add and access entries in the array. This is better than just making the array global because if your program grows in complexity you can hide the actually implementation of the array from all users of the class. So if you need to convert to a more sophisticated method of storing each item you can change the underlying code and it will not affect anywhere else in the application. OOP let's you do this because the application will always use the clist class to access the data. And you did not change the actual interface of clist.

Here are some examples of why application requirements could force you to abandon the array implementation:

- You need to sort the entire list by some field in class c
- The array goes very large and needs to be cached
- You need fast access to a single entry based on a value not index
MFC kills cross platform compatibility.
you can use C++'s list class for most things you can use clist for.