home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.sgi
- Path: sparky!uunet!stanford.edu!CSD-NewsHost.Stanford.EDU!news
- From: philip@ziggy.stanford.edu (Philip Machanick)
- Subject: Re: C++ member pointer error
- Message-ID: <1992Jul29.030651.5087@CSD-NewsHost.Stanford.EDU>
- Sender: news@CSD-NewsHost.Stanford.EDU
- Reply-To: philip@ziggy.stanford.edu (Philip Machanick)
- Organization: CS Department, Stanford University, California, USA
- References: <1992Jul28.184745.2854@babbage.ece.uc.edu>
- Date: Wed, 29 Jul 1992 03:06:51 GMT
- Lines: 75
-
- In article <1992Jul28.184745.2854@babbage.ece.uc.edu> tmcbraye@snert.ece.uc.edu
- (Tim McBrayer) writes:
- > I'm trying to initialize a pointer to a class method and then call a function
- > through said pointer. I'm working in C++ on a SGI IRIS. As far as I can
- > tell, I'm accessing the pointer properly, according to Stroustrup's book.
- > However, when I compile with "CC junk.cc", I get:
- >
- > "junk.cc", line 21: error: object missing in call through pointer to
- > member function
- >
- > The most frustrating part of this is that the code works under g++ on a
- > Sparcstation. Are there any known bugs with SGI C++ with IRIX 4.0.1? Does
- > anyone have any idea what 'object' is being referred to in the error
- > message?
-
- I don't think this is a bug. I had the same results on a DECstation with cfront
- ported directly from AT&T source. A call to a member function (unless it is
- declared static) must be made through an object of the class _where the
- member function is defined_. Although it appears you are calling the function
- from an object of the appropriate class, this is not in fact so because under
- other circumstances e.fn () may be called from a different place or even be
- called after the original object is destroyed. (Do you think the semantics of
- the line
-
- e.fn = (fnptr) &V::print;
-
- should depend on the fact that this has happened inside a member function of
- class V?)
-
- If you want to ask a language lawyer for a more definitive answer post to
- comp.lang.c++.
-
- See modifications below for syntax (I found this in first edition of Lippman's
- C++ primer p. 214) - I don't usually use this feature:
-
- > #include <iostream.h>
- >
- > class V{
- > public:
- > int i;
- > V() { i = 0;};
- > void run();
- > char * print(int x) {cout << "x is " << x << endl; return "Foo!";};
- > };
- >
- > struct E {
- > typedef char * (V::*fnptr)(int);
- > fnptr fn;
- > E() { fn = 0;};
- > };
- > void V::run() {
- > E e;
- > e.fn = (fnptr) &V::print;
- > i = 42;
- > cout << (e.fn)(i) << endl; //ERROR: (*(e.fn))(i) gives the same error
- // this doesn't work because a non-static member function needs an object
- // of its own class (the "this" pointer)
-
- // replace the above by:
- cout << (this->*(e.fn))(i) << endl;
- > };
- >
- > int main(){
- > V v;
- > v.run();
- > cout << v.print(5) << endl;
- > }
- output:
- x is 42
- Foo!
- x is 5
- Foo!
- --
- Philip Machanick
- philip@pescadero.stanford.edu
-