home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: gnu.g++.help
- Path: sparky!uunet!stanford.edu!ames!haven.umd.edu!darwin.sura.net!spool.mu.edu!caen!destroyer!ubc-cs!unixg.ubc.ca!kakwa.ucs.ualberta.ca!acs.ucalgary.ca!edstrom
- From: edstrom@hsc.ucalgary.ca (John Edstrom)
- Subject: Re: how to do this?
- Message-ID: <92Sep13.170150.28707@acs.ucalgary.ca>
- Sender: news@acs.ucalgary.ca (USENET News System)
- Date: Sun, 13 Sep 92 17:01:50 GMT
- References: <92Sep10.203359.4881@acs.ucalgary.ca> <1992Sep12.160053.28778@isy.liu.se>
- Nntp-Posting-Host: elmer.hsc.ucalgary.ca
- Organization: Neuroscience Division, U of Calgary School of Medicine, Calgary, Alberta, Canada
- Keywords: pointer to member
- Lines: 106
-
- In article <1992Sep12.160053.28778@isy.liu.se> svan@isy.liu.se writes:
- >> What is wrong with the
- >> following constructions and how can I do it in g++?
- >>
- >> ----------
- >>
- >> class a {
- >> public:
- >> int x();
- >> };
- >>
- >> int a::x() { return 1; }
- >>
- >> main() {
- >> printf("%x\n", (int(*))a::x);
- >> }
- >
- >You're trying to cast a pointer to member to a pointer to object
- >type. That can't be done according to the standards
- >documentation I've read over C++ (Annotated C++ Ref. Man. by
- >Ellis & Stroustrup).
-
- To a member function, not just a member. It can be done because I
- encountered it while trying to port some code from AT&T C++ to g++.
- It just doesn't seem to be possible in g++.
-
- >Pointer to members may even not be explicitly converted to void*!
- >
- >The reason for this is that pointers to members may be
- >a structure containing both addresses and offsets. There is
- >no point in allowing a cast to any ordinary pointer.
- >
-
- There is if you want to choose different functions at a certain point
- in a program depending on current circumstances. Sorting elements by
- different criteria, for example.
-
-
- The workaround I've discovered is as follows :
-
- typedef int (a::*afp)( void ); // can not do it without typedef and
- // using intermediate variables
- class a {
- public:
- int a(void);
- int b(void);
- int c(void);
- };
-
- int a::a(void) { return 1; }
- int a::b(void) { return 2; }
- int a::c(void) { return 3; }
-
- main() {
- afp y = a::x;
-
- printf("%x\n", y); // this gives me the address of the member
- // function
- }
-
-
- But still I can't execute the function directly like its seems I
- should be able to:
-
- inline int a::do_something( afp x ) { return x(); } // no way
-
-
- The only way I can get it to go is to have another member function
- like:
-
- int a::executor(cfp x) {
- if (x == a)
- return a();
- if (x == b)
- return b();
- if (x == c)
- return c();
- }
-
-
- Where to get the value from a passed member function pointer the usage
- is:
-
- inline int a::do_something(afp x ) { return executor(x); }
-
-
- I know this is ugly and stupid and I hope there is a better way.
-
-
- >O----------------------------O----------------------------------O
- >| Jonas Svanberg | Email: svan@isy.liu.se |
- >| Div. of Information Theory O----------------------------------O
- >| Department of E.E. (ISY) | "And Noah said to the animals: |
- >| Linkoping University | - Go out and multiply! |
- >| S-581 83 Linkoping | The snake said: |
- >| SWEDEN | - But how can I? I'm an adder!" |
- >O----------------------------O----------------------------------O
- >
-
-
- JE
- --
- RM 2104, HSc Building, Div. Neuroscience
- U. Calgary School of Medicine, 3330 Hospital Drive NW
- Calgary, Alberta T2N 3Y4
- (403) 220 4493 (wk)
-