Topic: overloading []
Author: bjin@silver.ucs.indiana.edu (bong guk jin)
Date: Thu, 21 Apr 1994 20:22:05 GMT Raw View
Can anyone help me write a code to overload [] operator?
There are bunch of books mentioning this for 1D array but I cannot
find one for multi-dimension array, and I am a novice in c++.
My problem is :
class XXX {
public:
.........
private:
char m[10][10];
}
I know if m were 1D array, then it should look like
inline char& XXX::operator[](ind i) { return m[i];}
But in the case of 2D array, how can I overload [] operator?
Thanks in advance...
Author: raspar@ccmail.monsanto.com
Date: Fri, 22 Apr 1994 18:09:37 GMT Raw View
In article <CoMLwu.n2B@usenet.ucs.indiana.edu>, <bjin@silver.ucs.indiana.edu>
writes:
>
> Can anyone help me write a code to overload [] operator?
> There are bunch of books mentioning this for 1D array but I cannot
> find one for multi-dimension array, and I am a novice in c++.
> My problem is :
>
> class XXX {
> public:
> .........
> private:
> char m[10][10];
> }
>
I don't know what the standard says, but in current implementations of C++ like
C-SET++ and BCC, you can't. Use the operator() instead, you can have an
arbitrary number of parameters.
Rodney
Author: b91926@fsgi01.fnal.gov (David Sachs)
Date: 22 Apr 1994 15:02:05 -0500 Raw View
bjin@silver.ucs.indiana.edu (bong guk jin) writes:
>Can anyone help me write a code to overload [] operator?
>There are bunch of books mentioning this for 1D array but I cannot
>find one for multi-dimension array, and I am a novice in c++.
>My problem is :
>class XXX {
>public:
> .........
>private:
> char m[10][10];
>}
>I know if m were 1D array, then it should look like
> inline char& XXX::operator[](ind i) { return m[i];}
>But in the case of 2D array, how can I overload [] operator?
You have to define the output of XXX::operator[] to be something
that can be the first argument of a subscript construct.
Possibly, you may be able to return a pointer to the desired row
of your final return type.
If this is not feasible, you will have to define a class, just for
the purpose of acting as a temporary return value for the first
operator[] and as the base object for the second operator[].
Author: swf@tdat.ElSegundoCA.NCR.COM (Stan Friesen)
Date: Fri, 22 Apr 94 13:58:34 PDT Raw View
This is getting to be FAQ.
In article <CoMLwu.n2B@usenet.ucs.indiana.edu>, bjin@silver.ucs.indiana.edu (bong guk jin) writes:
|> There are bunch of books mentioning this for 1D array but I cannot
|> find one for multi-dimension array, and I am a novice in c++.
There really isn't, at least not directly.
|> My problem is :
|>
|> class XXX {
|> public:
|> .........
|> private:
|> char m[10][10];
|> }
|>
|> I know if m were 1D array, then it should look like
|>
|> inline char& XXX::operator[](ind i) { return m[i];}
|>
|> But in the case of 2D array, how can I overload [] operator?
|>
The closest you can come is:
class XXX_row {
public:
friend class XXX
char & operator [] (int j) { return ptr[j]; };
private:
char *ptr;
XXX_row(char *p): ptr(p) {};
};
class XXX {
public:
XXX_row operator [] (int i) { return XXX_row(m[i]); };
.......
private:
char m[10][10];
};
That is, the operator[] for your class, XXX, must return an instance
of a proxy class that itself has an operator[] and refers to a
logical row in the original class.
--
swf@elsegundoca.ncr.com sarima@netcom.com
The peace of God be with you.