home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!ascent!eb
- From: eb@ascent.com (Ed Barton)
- Subject: Re: pointer to member in MSC 7.0
- In-reply-to: tac@rena.world's message of 5 Nov 92 18:38:53 GMT
- Message-ID: <EB.92Nov9085634@ascent.ascent.com>
- Date: 9 Nov 92 08:56:34
- References: <TAC.92Nov5183853@rena.world>
- Organization: Ascent Technology, Inc., Cambridge Massachusetts
- Lines: 76
-
-
- [I tried to respond to this via E-mail, but it bounced.]
-
- In article <TAC.92Nov5183853@rena.world> tac@rena.world (Telmo A. Carmo) writes:
-
- How does one implement a pointer to a member function
- in MSC 7.0
- I am not able to use pointers to derived class member functions
-
- here is a small example:
- ----------------------
- class Object;
- //#define mt long (Object::*)()
- class Object {
- public:
- int get() { return 2234;}
- long perform(long (Object::*)(),long);
- //long perform(mt,long);
- };
- //#undef mt
-
- typedef long (Object::*method)();
- typedef long (Object::*method1)(long);
-
- class AA : public Object {
- public:
- long test(long v);
- void Do(method,long);
- };
- long Object::perform(method m,long v)
- {
- method1 q = (method1)m;
- return (this->*q)(v);
- }
- void AA::Do(method m,long v)
- {
- perform(m,v);
- }
- long AA::test(long v)
- {
- printf("ld=%ld\n",v); return 0l;
- }
- main()
- {
- AA ole;
-
- method m = (method)&AA::test;
- /* ^-- ERROR */
- ole.Do(m,(long)23);
- return 0;
- }
- /*
- yy.cpp
- yy.cpp(47) : error C2642: cast to pointer to member must be
- from related pointer to member
- */
-
-
- You are actually attempting to lie to the compiler. A pointer of type
- long (Object::*)() can point to exactly one type of thing: namely, a
- member function of Object. But AA::test is NOT a member function of
- Object. It is a member function of AA.
-
- Look at it this way: If you store something in an (Object::*)()
- pointer, then you can invoke the pointer on anything of type Object,
- including those that are not also of type AA; but to invoke the
- pointer on non-AA objects would lead to disaster. Consequently, it is
- unsafe and violates the language definition to store AA:test into an
- (Object::*)() pointer, even in those cases where the compiler may
- allow you to do so. It is not hard to make examples (involving
- virtual functions and such) in which your program will crash if you do
- this, EVEN IF the argument is actually of type AA.
-
-
-
-
-