Multidimensional Arrays (2D)

Hi guys

I’ve been programming again a bit lately…

In my app I’d like to have two-dimensional arrays.

The first one should hold pointers to TextEditors:

typedef OwnedArray<TextEditor> RowArray; RowArray* tempRowArray; Array<RowArray*> editor2DArray;
Is this a good way to do it? Any better ideas?

How can I delete the 2D-array and all TextEditors in it?delete editor2DArray[i]; Is this right?

I need also a 2D-array that holds strings should I do it the same way?

OwnedArray<StringArray> my2DstringArray;
How can I delete this one (and all Strings in it)?

Is it possible to overload a double bracket operator to work like this:

String foo = my2DstringArray[i][j];

Thx for your help!
Cheers Luke

In essence you’re on the right track, but I’m puzzled by your usage of OwnedArray versus Array.

Are you really sure you would want your text editors to be owned by the array, given that you describe it as an array of pointers, not of instances.

Equally, why is your outer array an Array, not an OwnedArray?

All that said, in general, if I wanted a true 2D array of a known type, I’d normally just create a lightweight wrapper class to do it. It could still be built using JUCE’s array components if desired, though it might well be more efficient to just roll your own. Either way, the wrapper class can abstract all the funky indexing stuff, as well as ensuring that when allocating/pre-allocating array dimensions, that all elements are valid. You don’t want to have unbalanced rows for example, and you probably don’t want null pointers.

Thanks for your reply valley…

I’m writing a class that holds one of the 2D-array. It’s methods are taking care of adding rows nad columns of TextEditors and so on…

So sould I use something like this?

if I delete the 2D-array likethis:

Does it delete also all TextEditors that it holds?

Sory if my questions are newbie’ish but I’m a quite poor C++ programmer and I’ve never before worked with templates. And I’m also not very comfortable with pointers (even though I’m begining to love them…)

Cheers Luke

anyway take care of not deleting stack variables…

Ok if I have one of my 2D-array as member variable in a class like this:

class MyClass { ... ... OwnedArray<OwnedArray<TextEditor>> myEditorArray; };
So I don’t have to delete it in the destructor because I didn’t create it on the heap in the constructor (with new). I suppose it is created on the stack.
In this case “myEditorArray” is deleted automaticly if the object of “MyClass” is deleted.
But what with the containing TextEditors? Are they deleted as well?
Or do I need to delete them by myself? If so: How?


That’s fine, and the text editors will be automatically be deleted.

Ok… Thanks guys!
I’ll give it a try…