We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


Template array class stuff

migue asked
Medium Priority
Last Modified: 2010-04-01
I have this template array class decl:

// Base template 1D      
template <class Type, int low, int high,int D> class A1D
   A1D(void): element(new Type[high-low+1]) { }
   ~A1D() { delete [] element; }

   Type& operator[](const int i) {
          if(i<low || i>high) {
              cout << "Array out of range error !!" << endl;
        return element[i-low];

   operator Type*() {
   inline int begin() const {return low;}
   inline int end()   const {return high;}
   inline int dim()   const {return D;}
   //Type *element[high-low+1];
   Type *element;

// 2D      
template <class Type, int L1, int H1, int L2, int H2,int D>
class A2D : public A1D<A1D<Type,L2,H2,1>,L1,H1,D>

// 3D      
template <class Type, int L1, int H1, int L2, int H2, int L3, int H3, int D>
class A3D : public A1D<A2D<Type,L2,H2,L3,H3,2>,L1,H1,D>

In a program I declare the following:

A3D<int,0,5,0,5,0,5,3> arr;    // NOTE:  ( I know what you are thinking, but my compiler cannot handle default                                                   //                 template args..)

i also a have:

void foo(int a[][5][5])

I want to pass arr to foo, but the compiler complains of a type mismatch.

Watch Question

That's not suprising.

arr is a class.  It is a class that acts like an array, but it is a class.
foo takes an array.  

The two are not the same.

You could declare foo to take a A3D<int,0,5,0,5,0,5,3>  like

void foo(A3D<int,0,5,0,5,0,5,3> a)


nietod: I know the two are not the same. But can i code an operator for the class to take care of this problem ?

an operator to do what?  A conversion operator?  You can't realistically make a conversion operator because your class stores the data in the wrong format.  You could dynamically allocate memory of the right format and fill it it, but then you would have a memory leak.  You could use a proxy class to take care of the memory deletion, but then your conversion wouldn't work automatically.  So you would be forced to use an ugly syntax and it would be very innefficient.

I wouldn't recommend it.  I suggest you look into alternatives.  What is it that you are hoping to accomplish?

Or am I missunderstanding what you meant about an opperator?


What I am trying to accomplish is that I want the template array class to behave as much as possible to regular 'C' arrays. If I cannot pass it to a function expecting a n-dimensional array of the same type(int,char,double...) then I am out of luck.

I have tested this class...it has no memory leaks that I know.

It has no memory leaks at the moment.  But you would have a hard time writing a conversion operator that didn't have a memory leak.

I think you are out of luck.

Your options are...

1.  write fuctions that take  the class instead of the c array.
2.  write a fucntion to create a c-array from the class.  But you will have cleanup issues.
3.  redesign your class to be memory compatible with c arrays--this is a big change with some serious dissadvantages, but also some major advantages.  You will get great flexibilty at the price of many compile time error checks.  

Opption 1 is probably the best.
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview


thanks guys..I guess I took the wrong design approach.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.