Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!samsung!uunet!world!wmm From: wmm@world.std.com (William M Miller) Newsgroups: comp.lang.c++ Subject: Re: difference between friend and static member function Message-ID: <1991Feb27.175343.6038@world.std.com> Date: 27 Feb 91 17:53:43 GMT References: <1991Feb27.153602.19495@kodak.kodak.com> Organization: The World Public Access UNIX, Brookline, MA Lines: 32 cok@islsun.Kodak.COM (David Cok) writes: > 4) The compiler (Sun C++ 2.0 on a SparcStation) complains about > > static myclass operator+(myclass,myclass) > > saying > error: myclass::operator +() must take 0 or 1 arguments > > This seems to me to be a bug since there is no implied this argument for a > static member. Right or wrong? E&S section 13.4.2, page 333, says: "A binary operator may be declared either by a nonstatic member function (section 9.3) taking one argument or by a nonmember function taking two arguments." (A similar passage in 13.4.1 deals with unary prefix operators.) There's no provision for static member functions to be used for operator overloading. The compiler was correct to reject the declaration. To understand why, think about the definitions of operator member functions and static member functions. An expression "a + b," where "a" is an object of a class with a member operator+(), is, by definition, equivalent to "a.operator+(b)." However, if a static member function is invoked using the member selection syntax "a.smf()," the expression on the left side of the "." is not evaluated (E&S section 9.4, page 180). That's clearly not what you want to happen with operators. Obviously the rules could be rewritten to make static operator member functions behave differently from ordinary static member functions, but it's not clear, at least to me, whether it's worth changing. -- William M. Miller, Glockenspiel, Ltd. wmm@world.std.com