lilzz
Posts: 411
Joined: Sat Nov 30, 2013 5:27 pm

C++ arrays

Wed Aug 20, 2014 8:57 pm

1)
PtrArray<Array<int.*> ptrArray1(0,10)
how do I interprete the above?

2)DynamicArray<MString> MStringArray1(10, "foo");
if the Array1 is of Mstring why it can initialized with integer of 10?

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: C++ arrays

Wed Aug 20, 2014 10:55 pm

Hi lilzz, it would be helpful to know some context for your questions. Where are you getting these templated classes? From a quick search of this and your other questions I am guessing MLC++. Is that correct?
lilzz wrote:1)
PtrArray<Array<int.*> ptrArray1(0,10)
how do I interprete the above?
This does not appear to be correct. 1) there is only one > and two <. 2) int.* is not a type. Do you mean PtrArray<Array<int*> > ptrArray1(0, 10);
lilzz wrote:2)DynamicArray<MString> MStringArray1(10, "foo");
if the Array1 is of Mstring why it can initialized with integer of 10?
In your code snippet MStringArray1 (there is no variable Array1) is a DynamicArray<> of type Mstring. MStringArray(10, "foo") initializes the DynamicArray<MString> class and so calls the constructor of that (DynamicArray<MString>) class. I suspect given the name DynamicArray it can grow dynamically, but you can specify an initial size in the constructor. so MStringArray(10, "foo") will be initialized to hold 10 MString objects and each of these objects will be initialized with the value "foo".

lilzz
Posts: 411
Joined: Sat Nov 30, 2013 5:27 pm

Re: C++ arrays

Thu Aug 21, 2014 5:19 pm

AndyD wrote: 2)DynamicArray<MString> MStringArray1(10, "foo");
if the Array1 is of Mstring why it can initialized with integer of 10?
In your code snippet MStringArray1 (there is no variable Array1) is a DynamicArray<> of type Mstring. MStringArray(10, "foo") initializes the DynamicArray<MString> class and so calls the constructor of that (DynamicArray<MString>) class. I suspect given the name DynamicArray it can grow dynamically, but you can specify an initial size in the constructor. so MStringArray(10, "foo") will be initialized to hold 10 MString objects and each of these objects will be initialized with the value "foo".
OK, it's not a 2-dimensional array? Like below the ranNums with two numbers indicates the size of the 2-d array.

Code: Select all

Array<MRandom> ranGens(10);
   Array2<int> ranNums(10, 101);  // ranNums[i][j] stores the j-th random
                                  // number produced by the i-th generator.
                                  // ranNums[i][0] stores how many random
                                  // numbers have been generated so far. 
   for (int i=0; i<10; i++) {
      ranGens[i].init(SEED3);
      ranNums(i, 0) = 0;
   }

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: C++ arrays

Thu Aug 21, 2014 10:12 pm

lilzz wrote:OK, it's not a 2-dimensional array? Like below the ranNums with two numbers indicates the size of the 2-d array.
Probably not, but I can only guess the functionality of a template from a small snippet of code!
lilzz wrote:

Code: Select all

Array<MRandom> ranGens(10);
   Array2<int> ranNums(10, 101);  // ranNums[i][j] stores the j-th random
                                  // number produced by the i-th generator.
                                  // ranNums[i][0] stores how many random
                                  // numbers have been generated so far. 
   for (int i=0; i<10; i++) {
      ranGens[i].init(SEED3);
      ranNums(i, 0) = 0;
   }
Each of these templates are different. Again, it would be nice to have some information about this code as a whole. It is very hard to give you any useful information from these snippets!

My best guess would be that PtrArray<>, DynamicArray<> and Array<> all allow you to access a collection of objects as if they were a one-dimensional array. I would further guess that Array2<> behaves like a two-dimensional array. These are all separate templates with (probably) different implementations.

Return to “C/C++”