Solved

nested templates

Posted on 1998-10-08
8
227 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
Comment Utility
It would be throwing an exception, but you dissabled that.  Why?
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
Nietod: thanks for help, please email your othe proposed design solution. I added another 20 points.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now