Link to home
Start Free TrialLog in
Avatar of migue
migue

asked on

nested templates

#include<iostream.h>

template<class T> class Array {

      public:

        // used for throwing exceptions of this type
        class range{};

        Array(): data(0), sz(0) {}
        Array(unsigned size): sz(size), data(new T[size]) {}
        ~Array() { delete [] data;}

        const T& operator[](unsigned n) const {
            if(n >= sz || data == 0) {
                  //throw range();
                  //return((T&)0);
            }
            return(data[n]);
        }

        T& operator[](unsigned n) {
          if(n >= sz || data == 0) {
                //throw range();
                  //return((T&)0);
            }
            return(data[n]);
        }

        operator const T*() const {
          return(data);
        }

        operator T*() {
          return(data);
        }

      unsigned Top(void) const {
          return(sz);
        }

  private:
        T* data;
        unsigned sz;
        Array(const Array& a);
        Array& operator=(const Array&);
};

void main(void)
{
   Array< Array<int> > foo(2);

   cout << foo[0][0] << endl;

}
------------------------------------
Code compiles, but prog. gives access violation. I stepped thru with debugger. Default ctor is used, so no memory is init. I want to use this template for n dimentional arrays.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

The line

  Array< Array<int> > foo(2);

constructs 3 arrays.  foo is constructed as an array that contains 2 arrays.  (since a size was specified to its constructor, the default constructor is not used.).  However, the two arrays inside of foo's array are default constructed.  (When the Array class creates its array, it uses the default constructor on T, in this case when foo creates its array, it is used the default constrcutor of Array<int>.)  Thus Foo has two entries, but those entries have no entries, this the code

foo[0]

is fine, it returns an Array<int> with no entries, but

foo[0][0]

is not because it tries to access an entry in the empty array.

I hope this helps, let me know if you have questions.
Avatar of migue

ASKER

good explanation nietod, but I still need to do "foo[0][0]" . HOW do I feed the empty array?
I want to be able to access foo[][][][].....n dimensions.


First you need to size the inner arrays, you could add a SetSize() member, then do

 Array< Array<int> > foo(2);

foo[0].SetSize(2);
foo[1].SetSize(2);

Then you could do

foo[0][0]

That doesn't seem very convenient however. and you could have problems where some of the inner arrays are not sized to the same dimensions as the others.  But that is probably  the best you can do with this sort of design.

Does that help?  I can think of another approach where you don;t have to worrk about having the inner arrays sized, but ir requires adding a 2nd type of array class ans at least one other support class.  It makes things a bit more complex, but more automatic too.  I could elaborate.
Avatar of migue

ASKER

Nietod: thanks for help, please email your othe proposed design solution. I added another 20 points.
Some advice, in the future, don't accept answers until you have a working solution you are compfortable with.

Fist of all you will need two array template classes.  One class will be used as the innermost array.  It doesn't make any assumptions about the data it stores.  The second class is used for all the outer arrays.  It assumes the classes it stores is an array (either of the two array classes), and therefore it can assume certain members are present.  This will allow it to set the size of the inner class.  Make sense?
Now with this technique, you could set all the arrays to the same size, easily, like you could construct a [100][100][100] array by passing 100 to the outer array's constructor and it would pass it on to the next and the next.  However, you cannot do an array like [100][10][20], there is no way to pass the various values. The outer array's constructor must take a specific number of parameters.  But there is a way to make this work, if you need it.  It involves adding yet another class.  (You can start on the others in the meantime, this can be added later, if you want it.)