Path: utzoo!mnetor!uunet!husc6!sri-unix!sri-spam!distek4!mckenney From: mckenney@distek4..istc.sri.com (Paul E. McKenney) Newsgroups: comp.lang.c++ Subject: Re: Why doesn't c++ allow overloading member operators? Message-ID: <11831@sri-spam.istc.sri.com> Date: 21 Mar 88 19:57:48 GMT References: <11778@sri-spam.istc.sri.com> <8180005@eecs.nwu.edu> Sender: nobody@sri-spam.istc.sri.com Reply-To: mckenney@sri.com (Paul E. McKenney) Organization: SRI International Lines: 50 In article <8180005@eecs.nwu.edu> morrison@eecs.nwu.edu (Vance Morrison) writes: > The real reason you probably want overloading is to allow "controled >access" to instance variables. What would meet this need without overloading >the "." is for c++ to REDEFINE the "." operator for the class EXAMPLE so > example.member means example.member() > and if example.member() does not exist, then the "." means what it >used to. I know for a fact that AT&T c++ does NOT allow member fuctions >and member variables to have the same name so this would not break any >code. > SO HOW ABOUT IT?? > Vance Morrison > morrison@accuax.nmu.edu The only problem that I can see with this is if it is legal to pass member functions as parameters. The `C++ Programming Manual' claims that doing this is implementation-dependent (pg 276). I would have no problem stealing an implementation-dependent case, but I could imagine myself having a different attitude if I had written a large c++ application :-). If there is too much history out there to allow Vance's clever modification, perhaps the concept of `member operator' could be added to the current `member variable' and `member function' suite. For example, the declaration: struct s { int a { return (1) }; char b; } S; would declare these objects: o a structure `s'. o a postfix member operator `.a' that returns `int' when applied to an object of type `struct s'. o a postfix member operator `->a' that returns `int' when applied to an object of type `struct s *'. o a member variable `b' of type `char'. o a variable of type `struct s' named `S'. Then the compiler would decide which `.a' (or `->a') operator was to be invoked based on the type of the expression that it was applied to (just as for any other overloaded operator). Note that the declaration could be distinguished from member functions since there is no `()' after the name, and it could be distinguished from member variables because of the curly braces. This approach makes is unnecessary for the member operators to be in any way different from other operators. In particular, there is no need for any additional access to compile-time info, nor for any special compile-time constant requirements. Thanx, Paul