Re: new operator by Dan
Dan
Sat Feb 07 09:02:41 CST 2004
Hai Ly Hoang wrote:
> Hi,
> int *(*arr)[5];
> a is a pointer to an element (e1). e1 is an array of 5 integer pointer.
> Now i want to to allocate for arr. How to do that ?
>
> int *(**arr2)[3];
> and arr2 = new (????).
> arr2 is now an array of pointer p1. Each pointer p1 point to an object e.
> Each e1 is an array of pointers which type is integer pointer. Is it true ?
> What is the syntax of "new " operator ?
See John's post. C++ is a low level language. It only supports simple
constructs while high level languages support abstractions like what you
are trying to do. Abstractions are created with classes in C++, they
have to be written. That's why he suggested the MultiArray class.
You could:
int ar( int *ar, int a, int b ) { return ar[ a * b ]; }
#define ax( a, b ) au[ a * b ]
then:
int i= 3, j= 5;
int *au= new int[i*j];
and:
int h= ar( au, 2, 3 );
or:
int w= ax( 3, 4 );
or even:
int x= au[ 3 * 2 ]; //back to where you started!
But this is a very dangerous practice as well as very limited.
There is no bounds checking.
Start using classes in C++ or you will not be happy with the language.
It also means you can use a class that is applicable to your needs. If
you plan to do linear algebra with your arrays, you can use a class with
all the matrix math built in!
> Thanks alot !
Best, Dan.