5 dimensional array

seems to run find: But did i make any mistakes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>

using namespace std;
template < class Tc>
Tc T_chk( string& s)
{ return ""; }
template < class Tc>
Tc T_chk( Tc& t)
{ return 0; }

template <typename T>
class Darray
{
	int a,b,c,d,e,m3,m4,m5;
	T* array;
	
	public:
	Darray( int aa , int bb = 1 ,int cc = 1 , int dd = 1 , int ee = 1)
	{
		if (aa < 2) { aa = 2; }
		if (bb < 1) { bb = 1; }
		if (cc < 1) { cc = 1; }
		if (dd < 1) { dd = 1; }
		if (ee < 1) { ee = 1; }
		a = aa; b = bb; c = cc; d = dd; e = ee;
		m3 = a*b; m4 = m3*c ; m5 = m4*d;
		array = new T [a*b*c*d*e];
	}
	~Darray()
	{
		delete[] array;
	}
	bool chk( int& val , int& comp)
	{
		if ( val > -1 && val < comp )
			return true;
		cout << "out of bounds : ";
		return false;
	}
	T Get( int x1, int x2 = 0, int x3 = 0, int x4 = 0, int x5 = 0)
	{
		T ret;
		ret = T_chk<T> ( ret );
		if ( chk( x1 , a ) == false ) { return ret; }
		if ( chk( x2 , b ) == false ) { return ret; }
		if ( chk( x3 , c ) == false ) { return ret; }
		if ( chk( x4 , d ) == false ) { return ret; }
		if ( chk( x5 , e ) == false ) { return ret; }
		
		cout << (x1 + x2*a + x3*m3 + x4*m4 + x5*m5) << " : ";
		return array[ x1 + x2*a + x3*m3 + x4*m4 + x5*m5 ];
	}
	
	void Set( T val, int x1, int x2 = 0, int x3 = 0, int x4 = 0, int x5 = 0 )
	{
		bool pass = true;
		if ( chk( x1 , a ) == false ) { pass = false; }
		if ( chk( x2 , b ) == false ) { pass = false; }
		if ( chk( x3 , c ) == false ) { pass = false; }
		if ( chk( x4 , d ) == false ) { pass = false; }
		if ( chk( x5 , e ) == false ) { pass = false; }
		
		if (pass == true)
			array[ x1 + x2*a + x3*m3 + x4*m4 + x5*m5 ] = val;
	}
};
		
int main()
{
	Darray<string> ds(4,8);
	ds.Set( "string" , 2 , 4 );
	cout << ds.Get( 5 , 0 ) << endl;
	cout << ds.Get( 2 , 4 ) << endl;
	Darray<int> d(5,7,5,5);
	cout << d.Get( 5 , 0 , 0 ) << endl;
	cout << d.Get( 0 , 1 , 0 ) << endl;
	cout << d.Get( 4 , 6 , 0 ) << endl;
	cout << d.Get( 0 , 0 , 1 ) << endl;
	cout << d.Get( 4 , 6 , 4 ) << endl;
	cout << d.Get( 0 , 0 , 0 , 1 ) << endl;
	cout << d.Get( 4 , 6 , 4 , 1) << endl;
	return 0;

What you've done seems ok as far as it goes.

You don't cope with copies. You should either implement or disable the copy constructor and assignment operator.

If T is not a built in type, the copies of T in Get/Set could be expensive.

You don't support a const interface.

Why a 5D array? Why not more dimensions or less? Could it be even more general?
Topic archived. No new replies allowed.