Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

operator[][]

36 views
Skip to first unread message

asetof...@gmail.com

unread,
May 5, 2015, 6:00:48 PM5/5/15
to
Is it possible to define operator operator[][]?
(and operator[][][] and operator[][][][])

T& operator[](int i)
{}
Seems the only that compile
Message has been deleted

red floyd

unread,
May 5, 2015, 6:12:29 PM5/5/15
to
Only if T is either a pointer, array, or some type that has operator[]()
defined.

asetof...@gmail.com

unread,
May 5, 2015, 6:24:10 PM5/5/15
to
Stefan wrote:
<< it
can be demonstrated by a one-liner (80 characters):

struct s{ s & operator[]( int ){ return *this; }}; int main(){ s{}[ 0 ][ 0 ]; }
>>
What does it mean s{}?

I think i can easily resolve this
call as function M.elm(i, j)
at place of M[i][j]...
Message has been deleted
Message has been deleted

Nobody

unread,
May 6, 2015, 3:48:46 AM5/6/15
to
On Tue, 05 May 2015 15:00:38 -0700, asetofsymbols wrote:

> Is it possible to define operator operator[][]?
> (and operator[][][] and operator[][][][])

No. But you can define operator[] such that its return type supports
subscripting (either a pointer or a class which itself implements operator[]).

Alternatively, you could use operator() for array acess, so a[i][j][k]
becomes a(i,j,k).

Juha Nieminen

unread,
May 6, 2015, 4:47:47 AM5/6/15
to
To achieve that syntax, T must be something that itself supports
operator[]. It's perfectly possible, you just have to define it
more recursively.

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---
0 new messages