heintze.fmcsse.enet.dec.com (Sieg Heintze) (01/30/91)
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 <iostream.h>
#elif defined(__ZTC__)
#include <stream.hpp>
#elif defined (__GNUC__)
#include <stream.h>
#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-2184mike@taumet.com (Michael S. Ball) (01/31/91)
In article <19587@shlump.nac.dec.com> heintze.fmcsse.enet.dec.com (Sieg Heintze) writes: >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[]. See section 13.4.4 for function call and 13.4.5 for subscript. >(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. Allowing extra arguments for operator[] is a bug in Turbo C++. Have you tried to invoke such an operator? Since [] is binary I don't see how you can do it (barring "a.operator [](b,c)"). If you write a[b,c], the expression b,c is taken as a comma expression which is one operand of a binary operator. -Mike- -- Michael S. Ball mike@taumet.com TauMetric Corporation (619)697-7607
jimad@microsoft.UUCP (Jim ADCOCK) (02/06/91)
In article <19587@shlump.nac.dec.com> heintze.fmcsse.enet.dec.com (Sieg Heintze) writes: >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. See ARM page 335-337. Overloaded operators are suppose to follow the same syntax as built-in operators. Thus, op[] can only have two operators -- foo[bar] has a first [implied] parameter of foo, and a second parameter of bar. Op[] must be a non-static member function. The usage form of op() is: primary-expression(optional-expression-list) op() must also be a non-static member function. Thus: int X::operator()(); foo X::operator()(int x, double y); foo X::operator()(int x); double X::operator()(int a, int b, int c, int d, int f, double x); should all be accepted. ARM refers to op() as being a "binary operator" -- which might have confused some implementors -- though ARM does give examples of op() being used with totals of three and four parameters.