This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]