Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!elroy.jpl.nasa.gov!decwrl!pa.dec.com!shlump.nac.dec.com!fmcsse.enet.dec.com!heintze From: heintze.fmcsse.enet.dec.com (Sieg Heintze) Newsgroups: comp.lang.c++ Subject: How many arguments for operator() and operator[]? Summary: How many arguments can operator() and operator[] take? Keywords: overloading, operator, arguments Message-ID: <19587@shlump.nac.dec.com> Date: 30 Jan 91 02:15:33 GMT Sender: newsdaemon@shlump.nac.dec.com Organization: Digital Equipment Corporation Lines: 114 Could someone point me to the place in the Stroustrup and Ellis C++ Reference manual where they define the number of arguments permitted when defining a member function that overloads operator() or operator[]. I have tried this program on three compilers and all three behave differently. I'm wondering how other C++ compilers behave and just what the correct behavour is. (1) ZORTECH only allows 0 or 1 argument for overloading operator (). (2) GNU C++ allows two arguments for overloading operator() but not operator[]. (3) TURBO C++ allows two arguments for operator() and operator[]. I assume that because it allows two, it will allow larger numbers of arguments also. I'm told the point is that overloaded operators (including []) look the same as the builtin versions, so there aren't any differences permitted. (Check '5.2.1 for the definition of E1[E2] as *((E1)+(E2)) you will not see more than 1 argument.) Ok, what about operator()? Thanks, Sieg // Experiment with multidimensioned arrays // #ifdef __TURBOC__ #include #elif defined(__ZTC__) #include #elif defined (__GNUC__) #include #endif class array { private: float* contents; int hi0; int hi1; int lo1; int lo0; int m0; int m1; public: array(int low0, int hig0, int low1, int hig1) { hi0 = hig0; lo0 = low0; hi1 = hig1; lo1 = low1; m0 = hi0-lo0+1; m1 = hi1-lo0+1; contents = new float[m0 * m1]; } float& operator()() { return contents[0]; } float& operator()(int row) { if ((row >= lo1) && (row <= hi1)) return contents[m0*(row-lo1)]; cout << "Index range error"; return contents[0]; } float& operator[](int row) { if ((row >= lo1) && (row <= hi1)) return contents[m0*(row-lo1)]; cout << "Index range error"; return contents[0]; } // Zortech Compiler won't swallow 3 operands on an operator float& operator()(int row, int col) { if ((row >= lo1) && (row <= hi1) && (col >= lo0) && (col <= hi0)) return contents[m0*(row)+col]; cout << "Index range error"; return contents[0]; } // Neither ZTC or GNUC will swallow this #ifndef __GNUC__ float& operator[](int row, int col) { if ((row >= lo1) && (row <= hi1) && (col >= lo0) && (col <= hi0)) return contents[m0*(row)+col]; cout << "Index range error"; return contents[0]; } #endif }; int main() { array a(1,10, 1, 12); cout << " begin main program "; a() = 3.14159; a(1) = 44.2232; a(4,5) = 323.232; #ifdef __GNUC__ cout << a() << " " << a(1) << " " << a(4,5) << '\n'; #else a[4,3] = 423.33; cout << a() << " " << a(1) << " " << a(4,5) <<" "<< a[4,3] << '\n'; #endif return 0; } heintze@genral.enet.dec.com Digital Equipment Corporation 1110 Chapel Hills Drive CXN2-2/35 Colorado Springs, CO 80920-3995 719-260-2184