🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1838

Re: Semantics operator->()

#1838
From: Ajay Lunawat
Date: Thu, 24 Aug 1995 16:31
67 lines
2480 bytes
In article <41gq8b$1jm@tango.cs.wustl.edu>, schmidt@cs.wustl.edu (Douglas C. Schmidt) writes:
|> 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?

    Sun compiler seem to be doing right thing here.
    Consider this
    
    Foo *x = new Foo(10);
    x->f();  // f() is one of the public interface/function for Foo class.

    Now if *x.operator->() returned a pointer to int, the statements will be
    equivelent of:
    int *i = new i(10);
    i->f();
    
    Which of course should not work. That is operator->() should return 
    pointer to a User defiend type(struct/class).

Ajay

[ 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. ]
Technical Details
Message-ID: <41i9hu$7io@engnews2.Eng.Sun.COM>
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
From: ajay@unison.com (Ajay Lunawat)
Newsgroups: comp.std.c++
Subject: Re: Semantics operator->()
Date: 24 Aug 1995 16:31:58 GMT
Organization: ?
Sender: usenet@unison.com (news poster)
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <41i9hu$7io@engnews2.Eng.Sun.COM>
NNTP-Posting-Host: taumet.eng.sun.com
Content-Type: text
X-Nntp-Posting-Host: zephyr.unison.com
Content-Length: 1704
X-Lines: 63
Originator: clamage@taumet