Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Variable-size array in a class?

Variable-size array in a class?
Thread Tools
Junior Member
Join Date: Dec 2001
Location: My own personal purgatory
Status: Offline
Reply With Quote
Jul 27, 2002, 11:00 AM
 
I suspect that the answer is no, but I thought I'd ask anyway: I'm creating a class that has, as one of its instance variables, a 2-dimensional array of ints. I'd like the array to be differently sized in different instances of the class, if possible. Is there a way to do this without having to resort to the (far higher-level than I need) NSMutableArray class? I tried using a declaration like "int myArray[][];" in the header file, but that gave a compile-time error.

Any help is appreciated.
"A scientist can discover a new star but he cannot make one. He would have to ask an engineer to do it for him."
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Jul 27, 2002, 01:37 PM
 
You could do something like the following:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">#include &lt;vector&gt;

class MyClass {
public:
MyClass (int arraySize)
{ _array.reserve(arraySize); };

protected:
vector&lt;int&gt; _array;
};</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">To add an item to the back of the array, use _array.push_back(someInt). To get the number of elements, use _array.size(). Access the elements of your array using _array[someIndex].

Does this do the trick?

<small>[ 07-27-2002, 02:41 PM: Message edited by: smeger ]</small>
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
Forum Regular
Join Date: Sep 2000
Status: Offline
Reply With Quote
Jul 27, 2002, 03:38 PM
 
It sounds like (from the mention of NSMutableArray) this is being done in Objective-C.

You could declare the variable in your header like this:

</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">@interface MyClass : NSObject
{
int **_theArray;
}
@end</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Then you would need to allocate the array and each column of it in your init: implementation, and free each column and the array itself in your dealloc: implementation.

[Edit: Bad keystroke posted it before I was finished]

<small>[ 07-27-2002, 04:47 PM: Message edited by: Marshall ]</small>
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 11:15 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2