Article View: gnu.g++.bug
Article #1000pointer to member function
From: Walter Gillett
Date: Wed, 30 Sep 1992 20:12
Date: Wed, 30 Sep 1992 20:12
54 lines
2064 bytes
2064 bytes
Version of GNU: GNU C++ version 2.2.2 (sparc) compiled by GNU C version 2.2.2.
Platform: SPARCstation 2, SunOS 4.1.2
Complete input file: shown below
Command args: g++ -o ptr -g -I/home/walter/gcc/lib/sun4/g++-include -c ptr-ref.cc
The code shown below produces a seg fault; I think it should work
fine. It compiles with no errors or warnings. It corresponds to the
file ptr-ref.cc in the command line shown above. The idea is that "f"
is a pointer to the member function "inc". Member function "hawk"
dereferences the f pointer in order to call "inc". Note that the
"this" pointer in the definition of hawk should be redundant; I added
it in order to appease ObjectCenter (formerly Saber-C++). With that
change, the code loads and executes properly in ObjectCenter but not
in g++. This is the first example I've found where ObjectCenter has
been more robust than g++. I know that you folks don't have the
resources to provide support, but if you have any thoughts on this
topic I'd love to hear them. In particular, if there is in fact a bug
here, that would be of interest. I believe that the code below should
be legal (with or without the "this" pointer), but a lot of the
compilers I've tried don't like it very much.
#include <iostream.h>
class mouse
{
public:
int hawk();
mouse::mouse();
int mouse::inc(int x);
int (mouse::*f)(int);
private:
};
int mouse::inc(int x)
{
return x+1;
}
mouse::mouse()
{
f = &mouse::inc;
};
int mouse::hawk()
{
return (this->*f)(33);
};
int main()
{
mouse m;
cout << m.hawk();
};
Technical Details
Message-ID:
<9209302012.AA16487@madagascar.YP.matlab>Path:
arc1-ca.pugleaf.net!sparky!uunet!convex!linac!pacific.mps.ohio-state.edu!cis.ohio-state.edu!mathworks.COM!walterShow Full Headers
Complete Article Headers
Newsgroups: gnu.g++.bug Path: arc1-ca.pugleaf.net!sparky!uunet!convex!linac!pacific.mps.ohio-state.edu!cis.ohio-state.edu!mathworks.COM!walter From: walter@mathworks.COM (Walter Gillett) Subject: pointer to member function Message-ID: <9209302012.AA16487@madagascar.YP.matlab> Sender: gnulists@ai.mit.edu Organization: GNUs Not Usenet Distribution: gnu Date: Wed, 30 Sep 1992 20:12:31 GMT Approved: bug-g++@prep.ai.mit.edu Lines: 55