Topic: Question about the validity of forming and using member pointers to protected members


Author: johnchx2@yahoo.com
Date: Tue, 14 Feb 2006 21:16:45 CST
Raw View
Christoph Schulz wrote:

> Greg Herlihy wrote:
>>
> > This code is legal only in the sense that it will compile. The behavior
> > of its execution however is undefined. It is undefined because the
> > Derived::call_f() routine applies a member pointer of class Derived

No, it really doesn't.  The trick is to realize that the expression:

  &Derived::f

has type void(Base::*)()  (i.e. "pointer-to-member-fcn-of-Base taking
no parameter and returning void.")

If reading 5.3.1/2 isn't sufficient, notice that the compiler allows
you to apply the result of &Derived::f to a pointer of type Base*:

  void call_f (Base *base)
  {
      (base->* (&Derived::f)) ();
  }

Now try this:

   void call_f (Base *base)
   {
      void (Derived::* pmd)() = &Derived::f; // implicit conversion
      (base->* (pmd)) ();                    // error
   }

That is, once you've got an actual pointer-to-member-of-Derived, you
can no longer use it with at Base*.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]