home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / cplus / 15969 < prev    next >
Encoding:
Text File  |  1992-11-09  |  2.4 KB  |  88 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!ascent!eb
  3. From: eb@ascent.com (Ed Barton)
  4. Subject: Re: pointer to member in MSC 7.0
  5. In-reply-to: tac@rena.world's message of 5 Nov 92 18:38:53 GMT
  6. Message-ID: <EB.92Nov9085634@ascent.ascent.com>
  7. Date: 9 Nov 92 08:56:34
  8. References: <TAC.92Nov5183853@rena.world>
  9. Organization: Ascent Technology, Inc., Cambridge Massachusetts
  10. Lines: 76
  11.  
  12.  
  13. [I tried to respond to this via E-mail, but it bounced.]
  14.  
  15. In article <TAC.92Nov5183853@rena.world> tac@rena.world (Telmo A. Carmo) writes:
  16.  
  17.    How does one implement a pointer to a member function
  18.    in MSC 7.0 
  19.    I am not able to use pointers to derived class member functions
  20.  
  21.    here is a small example:
  22.    ----------------------
  23.    class Object;
  24.    //#define mt long (Object::*)()
  25.    class Object {
  26.    public:
  27.        int get() { return 2234;}
  28.        long perform(long (Object::*)(),long);
  29.        //long perform(mt,long);
  30.    };
  31.    //#undef mt
  32.  
  33.    typedef long (Object::*method)();
  34.    typedef long (Object::*method1)(long);
  35.  
  36.    class AA : public Object {
  37.    public:
  38.        long test(long v);
  39.        void Do(method,long);
  40.    };
  41.    long Object::perform(method m,long v)
  42.    {
  43.        method1 q = (method1)m;
  44.        return (this->*q)(v);
  45.    }
  46.    void AA::Do(method m,long v)
  47.    {
  48.        perform(m,v);
  49.    }
  50.    long AA::test(long v)
  51.    {
  52.        printf("ld=%ld\n",v); return 0l;
  53.    }
  54.    main()
  55.    {
  56.        AA ole;
  57.  
  58.        method m = (method)&AA::test;
  59.        /*  ^-- ERROR */
  60.        ole.Do(m,(long)23);
  61.        return 0;
  62.    }
  63.    /*
  64.    yy.cpp
  65.    yy.cpp(47) : error C2642: cast to pointer to member must be 
  66.                    from related pointer to member
  67.    */
  68.  
  69.  
  70. You are actually attempting to lie to the compiler.  A pointer of type
  71. long (Object::*)() can point to exactly one type of thing: namely, a
  72. member function of Object.  But AA::test is NOT a member function of
  73. Object.  It is a member function of AA.
  74.  
  75. Look at it this way: If you store something in an (Object::*)()
  76. pointer, then you can invoke the pointer on anything of type Object,
  77. including those that are not also of type AA; but to invoke the
  78. pointer on non-AA objects would lead to disaster.  Consequently, it is
  79. unsafe and violates the language definition to store AA:test into an
  80. (Object::*)() pointer, even in those cases where the compiler may
  81. allow you to do so.  It is not hard to make examples (involving
  82. virtual functions and such) in which your program will crash if you do
  83. this, EVEN IF the argument is actually of type AA.
  84.  
  85.  
  86.  
  87.  
  88.