Topic: overloading []=
Author: jimad@microsoft.com (Jim Adcock)
Date: 20 May 93 18:26:20 GMT Raw View
In article <C79zBu.By2@knot.ccs.queensu.ca> jinke@sparky.phy.queensu.ca (Ke Jin) writes:
> friend T& operator [](ARRAY& a, int pos) {
Yes, except ARM requires operator[] to be a member function.
Same difference, otherwise.
Author: David James Alexander Hanley <U34465@uicvm.uic.edu>
Date: Wed, 19 May 1993 00:33:54 CDT Raw View
I have created an array template to provide bounds checking like so:
template T
class ARRAY
{
int size;
T* data
public:
ARRAY( int s )
{
size = s;
data = new T[s];
};
T operator []( int pos )
{
if ( pos < 0 || pos >= s )
cerr << "Array out-of-bounds error!\n";
else
return( data[ pos ] );
};
This is nifty, and for my final compile I can take out the bounds checking
and make it normal arrays. However, I find I can't do this:
intarray[ a ] = i;
i have to use a function like this:
intarray.insert( a , i );
Which is okay, but I prefer the former. Anyone know how to do this?
dave
----------------------------
"The more one knows the less one beleives"
Author: ruiter@ruls41.LeidenUniv.nl (Jan Peter de Ruiter)
Date: Wed, 19 May 93 07:48:06 GMT Raw View
In article <93139.003354U34465@uicvm.uic.edu>, David James Alexander
Hanley <U34465@uicvm.uic.edu> writes:
[description of bound-checked array deleted]
|> However, I find I can't do this:
|>
|> intarray[ a ] = i;
|>
|> i have to use a function like this:
|> intarray.insert( a , i );
|>
|> Which is okay, but I prefer the former. Anyone know how to do this?
|>
|> dave
|> ----------------------------
|> "The more one knows the less one beleives"
What about using a reference parameter? Like this:
T& operator []( int pos )
{
if ( pos < 0 || pos >= s )
cerr << "Array out-of-bounds error!\n";
else
return( data[ pos ] );
};
I think that will do the trick.
Jan
Author: grumpy@cbnewse.cb.att.com (Paul J Lucas)
Date: Wed, 19 May 1993 12:52:16 GMT Raw View
Author: jinke@sparky.phy.queensu.ca (Ke Jin)
Date: Wed, 19 May 1993 13:11:53 GMT Raw View
In article <93139.003354U34465@uicvm.uic.edu> David James Alexander Hanley <U34465@uicvm.uic.edu> writes:
> I have created an array template to provide bounds checking like so:
>
>template T
>class ARRAY
>{
> int size;
> T* data
> public:
> ARRAY( int s )
> {
> size = s;
> data = new T[s];
> };
>T operator []( int pos )
> {
> if ( pos < 0 || pos >= s )
> cerr << "Array out-of-bounds error!\n";
> else
> return( data[ pos ] );
> };
>
>
>
>This is nifty, and for my final compile I can take out the bounds checking
>and make it normal arrays. However, I find I can't do this:
>
>intarray[ a ] = i;
>
>i have to use a function like this:
>intarray.insert( a , i );
>
>Which is okay, but I prefer the former. Anyone know how to do this?
>
>dave
>----------------------------
>"The more one knows the less one beleives"
You can try to overload the operator [] with a friend function instead of a `
member one. The followin code worked under gcc 2.2.2.
template <class T>
class ARRAY
{
int size;
T* data;
public:
ARRAY( int s ) { size = s; data = new T[s]; };
friend T& operator [](ARRAY& a, int pos) {
if ( pos < 0 || pos >=a.size);
cerr << "Array out-of-bounds error!\n";
else
return(a.data[pos]);
};
};
good luck.
Ke Jin
--
Andrew Ke Jin | I speak for myself not for God. | (613)-545-2723
Physics Department | This is not the thesis or a law. | jinke@sparky.
Queen't University | Don't blame if something wrong, | phy.queensu.ca
Kingston, K7L 3N6 | Just believe you're smart not me. |