Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

nested templates

Posted on 1998-10-08
8
Medium Priority
?
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 400 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
Industry Leaders: 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

604 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