whats wrong with pointer to member function
Matthias Oltmanns
Mathias.Oltmanns.Oltmanns@sysde.eads.net
Wed May 7 08:40:00 GMT 2003
Am Mit, 2003-05-07 um 10.28 schrieb Bhattiprolu Ravikumar-A18658:
> Hi Matthias,
>
> To my knowledge, in C++ to use function pointer to a member function,
> the member function has to be static member function. Try making foo as static.
>
No! Pointers to static member functions are *ordinary* pointers.
In this example, of course foo could be static. But in my real-life work
I must use non-static member functions.
Pointer to member functions are some kind of an offset used with the
operators: .* and ->*
Matthias
> regards,
> Ravi
>
> -----Original Message-----
> From: Matthias Oltmanns
> [mailto:Mathias.Oltmanns.Oltmanns@sysde.eads.net]
> Sent: Wednesday, May 07, 2003 1:46 PM
> To: gcc-help@gcc.gnu.org
> Subject: whats wrong with pointer to member function
>
>
> Hi,
>
> I'am confused while using pointer to member function. The compiler does
> not translate the following:
>
> ---- foo.cpp ---------------------------
>
> // Pointer to member function
> #include <iostream>
>
> class A
> {
> public:
> void foo()
> {
> std::cout << "Hi!" << std::endl;
> }
> };
>
> typedef void (A::*PMember) ();
>
> // VARIANT 1
> class B
> {
> public:
> void perform(A * pA)
> {
> PMember member = &A::foo;
> pA->*member();
> }
> };
>
> // VARIANT 2
> void perform_func(A * pA)
> {
> PMember member = &A::foo;
> pA->*member();
> }
> int main(int, char**)
> {
> A a;
> PMember member = &A::foo;
>
> // direct call
> a.foo();
>
> //VARIANT 3
> // call through member pointer
> a.*member();
>
> return 0;
> }
>
> > g++ foo.cpp
> foo.cpp: In member function `void B::perform(A*)':
> foo.cpp:21: must use .* or ->* to call pointer-to-member function in
> `member
> (...)'
> foo.cpp: In function `void perform_func(A*)':
> foo.cpp:28: pointer to member function called, but not in class scope
> foo.cpp: In function `int main(int, char**)':
> foo.cpp:40: pointer to member function called, but not in class scope
>
> My current knowledge about pointers to members is based on Stroustroup
> The C++ Programming Language, Third Edition, Chap. 15.5.
>
> As far as I can see, the code above is correct and i cannot understand
> what the compiler tries to tell me.
>
> Any help would be great.
>
> cu
> Matthias
>
>
> --
> Matthias Oltmanns
>
> Tel: 04421-1543-274
> mail: Mathias.Oltmanns.Oltmanns@sysde.eads.net
--
Matthias Oltmanns
Tel: 04421-1543-274
mail: Mathias.Oltmanns.Oltmanns@sysde.eads.net
More information about the Gcc-help
mailing list