[comp.lang.c++] overloading array indexing

peter@ethz.UUCP (Peter Beadle) (05/13/88)

I have been trying to overload the array indexing operation as part of
a simulator I am writing. I am getting nowhere fast. I have a
small program below and the C code it produces.

The translator produces a _tiny__vec function when I don't inline the
overload.  This function contains the correct code but it never gets
called in main.  Similarly when the overload is inlined nothing but a
reference to the name of the array is produced in main.  This is the
smallest example I can produce.

Anybody got any ideas.

Peter Beadle
peter@eiger.uucp
mcvax!cernvax!iis!peter

/* test program */
#include    <stream.h>
class tiny{
    int v;
public:
    tiny() {v=0;}
    void operator[](int i) { cerr << "in overloaded []\n"; }
};

main() {
    tiny    fred[5];

    fred[2];
}

/* the main program it produces */
int main (){ 
    _main(); 
    { 
        struct tiny _auto_fred [5];
        _vec_new ( (int *)_auto_fred , 5, 4, (int *)_tiny__ctor ) ;
        _auto_fred ;
    }
};

sarima@gryphon.CTS.COM (Stan Friesen) (05/15/88)

In article <451@ethz.UUCP> peter@ethz.UUCP (Peter Beadle) writes:
>
>I have been trying to overload the array indexing operation as part of
>a simulator I am writing. ...
>
>The translator produces a _tiny__vec function when I don't inline the
>overload.  This function contains the correct code but it never gets
>called in main.  ...
>
>/* test program */
>#include    <stream.h>
>class tiny{
>    int v;
>public:
>    tiny() {v=0;}
>    void operator[](int i) { cerr << "in overloaded []\n"; }
>};
>
>main() {
>    tiny    fred[5];
>
>    fred[2];
>}
>
	AH, I see the problem. The declaration creates a REAL array of 5
"tiny" objects called fred. Thus the expression "fred[2]" later indexes
into this array, generating a reference to a tiny object . You would have
to say "fred[2][2]" to get tiny::operator[], since that is only applied to
a "tiny" object, not an array of "tiny" objects. Look on page 182 of
Stroustrup's book and note that he declaes an instance of his "assoc" class,
which has a [] operator, as "assoc vec(512)" not "assoc vec[512]".
-- 
Sarima Cardolandion			sarima@gryphon.CTS.COM
aka Stanley Friesen			rutgers!marque!gryphon!sarima
					Sherman Oaks, CA

jss@hector.UUCP (Jerry Schwarz) (05/15/88)

In article <451@ethz.UUCP> peter@ethz.UUCP writes:
>
>I have been trying to overload the array indexing operation as part of
>a simulator I am writing. I am getting nowhere fast. 
>
>

The problem is that in the example subsrcipts a tiny*, rather than a
tiny.  Subscripting of pointers in C++ has the builtin meaning and
never invokes a defined operator. The operator[] member is used when
a value of the class type is subscripted.

 class tiny{
     int v;
 public:
     tiny() {v=0;}
     void operator[](int i) { ... }
	// Used when subscripting a "tiny"
 };

 main() {
     tiny    fred[5];
	// array of 5 tiny's
	
     fred[2];
	// equivalent to *(&fred[0]+2), whose evaluation requires
	// no code.

     fred[2][99] ;
        // calls fred's subsrcipting operator, with this==&fred[2].
 }


Jerry Schwarz
Bell Labs, Murray Hill