Solved

nested templates

Posted on 1998-10-08
8
231 Views
Last Modified: 2010-04-01
#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.
0
Comment
Question by:migue
  • 6
  • 2
8 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1174661
It would be throwing an exception, but you dissabled that.  Why?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174662
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.
0
 

Author Comment

by:migue
ID: 1174663
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.


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:nietod
ID: 1174664
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.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1174665
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.
0
 

Author Comment

by:migue
ID: 1174666
Nietod: thanks for help, please email your othe proposed design solution. I added another 20 points.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174667
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?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174668
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.)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Embarcadero C++ Builder XE2 TDateTime 8 80
Compile GLUT with Visual Studio 2015 1 204
computer science syllabus 3 100
How do i run a c++ file? 15 51
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question