Assignment takes two forms for virtual vectors. The problem
is to make the code work for saying vect[i] = 5
or
for saying vect1 = vect2
. The first form of
assignment is
double vdoub::operator = ( double d ) { if ( Garbage() ) { perror("VMS assignment error, source is Garbage(=)"); exit(1); } *cur_ele = d; vdirty( hdr ); return d; }
The functional form of vect[i] = 5
is
(vect.operator[](i)).operator=(5)
. First the
brackets operator is called, which sets the current index
to i. Since the bracket operator returns a reference to a
vdoub, the vdoub::operator=(5) is called. The current
element of the returned vdoub is set to 5. Next the buffer
is declared dirty, and the 5 is returned.
The other form of assignment copies a double vector to another. It disposes the left operand hdr if it already exists, then allocates a new hdr. Then it copies the contents from v to the new hdr. The code is given by
double vdoub::operator = ( vdoub &v) // copy vector v to vector t { if( v.Garbage() ){ perror("VMS copy error, source is Garbage"); exit(1); } if( !Garbage() ) vfree( hdr ); // replace hdr if t is active double *p; long numele = vele( v.hdr ); signature = SIGNATURE; *cur_ele = *v.cur_ele; cur_index = v.cur_index; if ( !(hdr = vmalloc( numele, sizeof( double )))) { perror(" VMS copy allocation error"); exit(1); } while( --numele >= 0 ) { if ( !(p = (double *) vread( v.hdr, numele))){ perror("VMS copy-access( read ) error"); exit(1); } if (!vwrite(hdr, numele,(char *)p)){ perror("VMS copy-access( write ) error"); exit(1); } } return *cur_ele; }
This code fragment really emphasizes how well the vdoub class insulates the user from the vmm system. You don't have to remember all of the arguments for vread, vwrite, and vmalloc. You also have a good deal of error checking embedded in the assignment operators.