🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by Douglas C. Schmidt Thu, 24 Aug 1995 03:56
Semantics operator->()
#1833
Author: Douglas C. Schmidt
Date: Thu, 24 Aug 1995 03:56
59 lines
1984 bytes
Hi,

	I've got a question about the behavior of operator->(), in
particular, can it return a pointer to a builtin type?

	For example, consider the following:
	
----------------------------------------
#include <stdio.h>

class Foo
{
public:
  Foo (int i): i_ (i) {}

  int *operator->()
  {
    return &this->i_;
  }

private:
  int i_;
};

int main ()
{
  Foo foo (10);
  printf ("%d, ", ++*(int *) foo.operator->());
  printf ("%d\n", ++*(int *) foo.operator->());
  // prints "11, 12"
  return 0;
}
----------------------------------------

	When I compile this with gcc it compiles and runs fine.
However, when I use SunC++ 4.0.1 I get the following error:

----------------------------------------
% CC foo.C
"foo.C", line 9: Error: Cannot have a return type of int* for Foo::operator->().
1 Error(s) detected.
----------------------------------------

	I tried to find a discussion of this in the ARM, but couldn't
find it discussed there.  Can someone please tell me if this is legal
in the draft ANSI/ISO C++ standard?

	Thanks,
	
		Doug
-- 
Dr. Douglas C. Schmidt 			(schmidt@cs.wustl.edu)
Department of Computer Science, Washington University
St. Louis, MO 63130. Work #: (314) 935-7538; FAX #: (314) 935-7302
http://www.cs.wustl.edu/~schmidt/

[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Semantics operator->()
#1847
Author: Joel Bender
Date: Fri, 25 Aug 1995 02:19
43 lines
1715 bytes
In article <41gq8b$1jm@tango.cs.wustl.edu> Douglas C. Schmidt  wrote:

>         I've got a question about the behavior of operator->(), in
> particular, can it return a pointer to a builtin type?

I can never seem to find where this is defined in the ARM, but
operator->() can only return something where operator->() is defined. 
Seems kinda strange, but it makes sense.

   struct S { int s; };

   S  *p;

   class C { S *operator->(void); };

   C  c;

In this case "c->s" is OK because "c.operator->()" returns something of type 
pointer to S and you would expect to have "p->s" work.  The way I think
about this now is "p->s" is the same as "p.operator->().s".

So "c->s" is the same as "c.operator->().operator->().s".

   int *px;

So *px is meaningful, but px-> is not defined, nor is "px.operator->()".


Note that "px.operator*()" IS defined, so you could do something like this:

   class Q { int& operator*(void); };

   Q q;

Now "*q" is an integer, the same one as refered to by "q.operator*()".

Does this help?

Joel
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads