Topic: Multi-dim array access operator


Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Thu, 9 Mar 1995 12:02:51 GMT
Raw View
mmoss@panix.com (Matthew Moss) writes:

>I am writing a class like this:
>
>class X {
> private:
>  double data[x][y];
>  ...etc...
> public:
>  X();
>  ...etc...
>};
>
>and I want to allow the user access to the values without allowing them access
>to the intermediate double* that arises from the first [] access. That is, they
>should be able to do:
>
> X a;
> cout << a[3][0];
>
>but they should not be able to do (or not be able to use):
>
> X b;
> cout << b[6];
>
>I can create a non-operator function, but I felt that operator[] would be
>nicer. Also, I suppose I can break up the 2-dimensional array into an array
>of some Array class that has [] defined, but I'd prefer keeping it in one
>class.

How about defining a proxy class?

 class X_row {
  double* my_array;
  X(double array) : my_array(array) {}
  friend class X;
 public:
  double operator [] (int index) const {
   return my_array[index];
  }
 };

class X {
 private:
  double data[x][y];
  ...etc...
 public:
  X();
  X_row operator[] (int r) const {
   return X_row(data[r]);
  }
  ...etc...
};

--
Fergus Henderson - fjh@munta.cs.mu.oz.au




Author: swf@elsegundoca.ncr.com (Stan Friesen)
Date: Mon, 6 Mar 1995 21:50:32 GMT
Raw View
In article <3j4ul4$gam@panix2.panix.com>, mmoss@panix.com (Matthew Moss) writes:
|> I am writing a class like this:
|>
|> class X {
|>  private:
|>   double data[x][y];
|>   ...etc...
|> };
|>
|> and I want to allow the user access to the values without allowing them access
|> to the intermediate double* that arises from the first [] access.
|> ...
|> nicer. Also, I suppose I can break up the 2-dimensional array into an array
|> of some Array class that has [] defined, but I'd prefer keeping it in one
|> class.
|>
Well, actually this is close to the best solution.

You provide an intermediate *access* class, and give it access
to the data in the first, and leave the data in the first class as is.

class slice {
  friend class X;
 private:
  double *row;

  slice(double *r): row(r) {};

  // make access to copy constructor and assignment illegal
  slice(const slice &sl): row(sl.row) {};
  operator=(const slice &);

 public:
  double& operator[] (int idx) { return row[idx]; };
};

class X {
 private:
  double data[x][y];
  ...etc...

 public:
  slice operator[](int idx) { return slice(data[idx]); };
  ...etc...
};


Any decent optimizer will pretty much optimize all of that away.

--
swf@elsegundoca.attgis.com  sarima@netcom.com

The peace of God be with you.




Author: mmoss@panix.com (Matthew Moss)
Date: 2 Mar 1995 12:18:28 -0500
Raw View
I am writing a class like this:

class X {
 private:
  double data[x][y];
  ...etc...
 public:
  X();
  ...etc...
};

and I want to allow the user access to the values without allowing them access
to the intermediate double* that arises from the first [] access. That is, they
should be able to do:

 X a;
 cout << a[3][0];

but they should not be able to do (or not be able to use):

 X b;
 cout << b[6];

I can create a non-operator function, but I felt that operator[] would be
nicer. Also, I suppose I can break up the 2-dimensional array into an array
of some Array class that has [] defined, but I'd prefer keeping it in one
class.

I don't suppose there is something like:
 double X::operator[](int r, int c); ???

Any ideas?
--
+---------------------------+-------------------------------------------------+
| INTERNET: mmoss@panix.com | Wimpy home page-> http://www.io.com/user/mmoss/ |
|           mmoss@io.com    | And yes!!! Ketchup *IS* a vegetable!!!    Fnord |
+---------------------------+-------------------------------------------------+