Topic: syntax for Pointer to member functions


Author: "Hardy" <hardeep.parmar@gmail.com>
Date: Thu, 1 Jun 2006 09:34:43 CST
Raw View
Hi All,
My question is about '&' pre-appended when assigning value to a pointer
to normal function.
Typical syntax is as follows.
class X
{
public:
int MyFunc(void) { return 42; }
};
typedef int(X::(*PMF))(void);
int main()
{
PMF pt = &X::MyFunc; // Note 1
return 0;
}
I know that there is a distinct difference between what & applied on
member function and & applied on normal function. The member function
returns much more info than say a normal function which just returns a
pointer to memory location of that function.
My question is why is '&' required. Can not a C++ compiler just deduce
that since it has a member function on RHS so while assigning it treats
it as if & was pre-appended to it.
This is because of all the compilers i know they either a) Treat
function RHS value with or without '&' identically or they flag an
error if you do not put '&'.
IMHO, '&' should be put only if the case with and without & are
different? Why is this not the case? Were there compilers in the past
which treated the case of with and without '&' valid and different?

Regards,
Hardy.

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: "Hardy" <hardeep.parmar@gmail.com>
Date: Thu, 1 Jun 2006 09:34:53 CST
Raw View
Hi All,
My question is about '&' pre-appended when assigning value to a pointer
to normal function.
Typical syntax is as follows.
class X
{
public:
int MyFunc(void) { return 42; }
};
typedef int(X::(*PMF))(void);
int main()
{
PMF pt = &X::MyFunc; // Note 1
return 0;
}
I know that there is a distinct difference between what & applied on
member function and & applied on normal function. The member function
returns much more info than say a normal function which just returns a
pointer to memory location of that function.
My question is why is '&' required. Can not a C++ compiler just deduce
that since it has a member function on RHS so while assigning it treats
it as if & was pre-appended to it.
This is because of all the compilers i know they either a) Treat
function RHS value with or without '&' identically or they flag an
error if you do not put '&'.
IMHO, '&' should be put only if the case with and without & are
different? Why is this not the case? Were there compilers in the past
which treated the case of with and without '&' valid and different?

Regards,
Hardy.

---
[ 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.comeaucomputing.com/csc/faq.html                      ]





Author: "Greg Herlihy" <greghe@pacbell.net>
Date: Fri, 2 Jun 2006 09:55:24 CST
Raw View
Hardy wrote:
> Hi All,
> My question is about '&' pre-appended when assigning value to a pointer
> to normal function.
> Typical syntax is as follows.
> class X
> {
> public:
> int MyFunc(void) { return 42; }
> };
> typedef int(X::(*PMF))(void);
> int main()
> {
> PMF pt = &X::MyFunc; // Note 1
> return 0;
> }
> I know that there is a distinct difference between what & applied on
> member function and & applied on normal function. The member function
> returns much more info than say a normal function which just returns a
> pointer to memory location of that function.
> My question is why is '&' required. Can not a C++ compiler just deduce
> that since it has a member function on RHS so while assigning it treats
> it as if & was pre-appended to it.
> This is because of all the compilers i know they either a) Treat
> function RHS value with or without '&' identically or they flag an
> error if you do not put '&'.
> IMHO, '&' should be put only if the case with and without & are
> different? Why is this not the case? Were there compilers in the past
> which treated the case of with and without '&' valid and different?

There is no implicit conversion in C++ from a member function to a
member function pointer like there is from a function to a function
pointer (in both C and C++). In other words, C++ preserves C's
function-to-pointer implicit conversion, so it is possible to use the
name of a function as its address. However, C++ does not extend this
implicit conversion to include member functions - so the address-of
operator (&) must be applied to a member function to obtain a pointer
to its address.

While implicit conversions are often useful - they can create risks as
well. Any kind of implicit operation has the potential to change (in
subtle and non-apparent ways) the meaning of an expression and thereby
cause the expression to have other than its intended effect. So I would
guess that the reason that no member function to member function
pointer implicit conversion exists in C++ - is simply that the
estimated risks outweighed the apparent benefit.

Greg

---
[ 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.comeaucomputing.com/csc/faq.html                      ]