Solved

nested templates

Posted on 1998-10-08
8
230 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
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.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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
I could not build boost code, 10 95
c++ syntax question 9 52
Gaming Software 1 19
Finding Divisors 5 19
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

838 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