Newsgroups: sci.image.processing
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!news.mathworks.com!news.alpha.net!uwm.edu!math.ohio-state.edu!jussieu.fr!univ-lyon1.fr!swidir.switch.ch!scsing.switch.ch!josef!hafner
From: hafner@ifi.unizh.ch (Michel Hafner)
Subject: Re: [Q] How to implement a 2D/3D Array/Matrix in C++?
Message-ID: <1995Jan10.110523.1013@ifi.unizh.ch>
Organization: University of Zurich, Department of Computer Science
References: <3es4q6$g8i@usenet.INS.CWRU.Edu>
Date: Tue, 10 Jan 1995 11:05:23 GMT
Lines: 38

In article <3es4q6$g8i@usenet.INS.CWRU.Edu> ery2@po.CWRU.Edu (Edwin R. Yeh) writes:
>
>Hi.
>
>In C, 2D image can be described as 2DArray[x][y] where I can declare it 
>as say float **2DArray. (Note that with the use of malloc() making it 
>a dynamic 2D matrix on the fly.) 
>
>I would like to access a 2D array in C++ in a similar way. 
>
>However, it appears that there's no class library support for
>multidimensional array (at least, not apparently in Borland C++ 4.5)
>Is there a way to construct 2D array without losing the convience
>of OOP? (I want to use Add(), new, destroy, and other class functions
>instead of the C style of allocation.) 

Define a class for matrices.
The 2d array is one big 1d array: float * 2DArray --> only one malloc used
The address of every line is in an additional array for pointers: float * *
pointers
Access is via operator overloading: []
inline float * operator [] ( long index ) {
      return ( pointers [ index ] ); };

for example:
FloatMatrix * fm;

fm = new FloatMatrix ( lines, columns,.. )
(*fm)[ i ][ j ] = 123.0;...

Or you can define a class for vectors and construct matrices as arrays of
vectors.
								Michel Hafner

   Michel Hafner             |\  /||\  /||     Email: hafner@ifi.unizh.ch  
   Dept. of Computer Science | \/ || \/ ||     Tel.:  +41 1 257 4317    
   University of Zurich      |    ||    ||____ Fax:   +41 1 363 0035    
   Switzerland_________________________________Telex:  817 251 unii ch
