Topic: pointers to *overloaded* member functions


Author: jason@cygnus.com (Jason Merrill)
Date: 1995/04/20
Raw View
>>>>> Jay Zipnick <jzipnick@best.com> writes:

> class c1
>    {
>    public:
>       int  flags() const;
>       void flags(int val);
>    };

> typedef int  (c1::*pmf1)() const;
> typedef void (c1::*pmf2)(int);

> int main()
>    {
>    //**** QUESTION 1: Assignments to pointer to member variables:
>    //
>    // All five compilers I have tested accept this. Can I assume
>    // that the standard calls for the compiler to disambiguate the
>    // overloaded function on the rhs?

Yes.

>                                         If not, how do I explicitly
>    // disambiguate the overloaded function names?
>    //-------------------------------------------------------------

>    pmf1 my_pmf1 = &c1::flags;    // int  flags() const;
>    pmf2 my_pmf2 = &c1::flags;    // void flags(int val)

>    //**** QUESTION 2: Alternate syntax?:
>    //
>    // On most compilers, both above and below compile the same
>    // (below is missing &). Is this just a quirk left over from C?
>    //-------------------------------------------------------------

>    pmf1 my_pmf1a = c1::flags;    // int  flags() const;
>    pmf2 my_pmf2a = c1::flags;    // void flags(int val)

This code is ill-formed; the '&' is required.

>    //**** QUESTION 3: Array initialization of pointer to member elements:
>    //
>    // Is anything wrong with this? One compiler doesn't like this!
>    //-------------------------------------------------------------

>    pmf1 array1[] = { &c1::flags };  // OK with 4 out of 5 compilers tested
>    pmf2 array2[] = { &c1::flags };  // OK with 4 out of 5 compilers tested

This is fine.

Jason





Author: jzipnick@best.com (Jay Zipnick)
Date: 1995/04/19
Raw View
// The below code demonstrates an unusual case of pointers to members,
// where the members are *overloaded* member functions.
//
// Different compilers have different ideas in this area. What does
// the draft standard say? Specifically, see the comments marked
// **** QUESTION 1/2/3.

class c1
   {
   public:
      int  flags() const;
      void flags(int val);
   };

   // pmf1 is a pointer to a const member function of c1,
   // which takes no parameters, and returns an int.
   //
   // pmf2 is a pointer to a member function of c1,
   // which takes an int, and returns void.

typedef int  (c1::*pmf1)() const;
typedef void (c1::*pmf2)(int);

int main()
   {
   //**** QUESTION 1: Assignments to pointer to member variables:
   //
   // All five compilers I have tested accept this. Can I assume
   // that the standard calls for the compiler to disambiguate the
   // overloaded function on the rhs? If not, how do I explicitly
   // disambiguate the overloaded function names?
   //-------------------------------------------------------------

   pmf1 my_pmf1 = &c1::flags;    // int  flags() const;
   pmf2 my_pmf2 = &c1::flags;    // void flags(int val)


   //**** QUESTION 2: Alternate syntax?:
   //
   // On most compilers, both above and below compile the same
   // (below is missing &). Is this just a quirk left over from C?
   //-------------------------------------------------------------

   pmf1 my_pmf1a = c1::flags;    // int  flags() const;
   pmf2 my_pmf2a = c1::flags;    // void flags(int val)


   //**** QUESTION 3: Array initialization of pointer to member elements:
   //
   // Is anything wrong with this? One compiler doesn't like this!
   //-------------------------------------------------------------

   pmf1 array1[] = { &c1::flags };  // OK with 4 out of 5 compilers tested
   pmf2 array2[] = { &c1::flags };  // OK with 4 out of 5 compilers tested

   return 0;
   }

// jzipnick@best.com