Passing address of a member function

From: J Solowiej (jarek_at_csc.albany.edu)
Date: 11/07/03


Date: Fri, 7 Nov 2003 14:04:08 -0500

Hi,

I am wondering: is it possible to pass pointer to member function (non
static) to initialize another class? For exmaple:

class X {
public:
  typedef double (*F)(double);
  X(F f) : f_(f) {}
private:
  F f_;
};

class Y {
public:
  Y() { t=1.0; X x(&Y::g);}
private:
  double t;
  double g(double x) {
    return(x+t);
  }
};

int main() {
  Y y;
  return(0);
}

wont's compile, g++ (3.2) gives the following messages:

In constructor `Y::Y()': no matching function for call to
`X::X(double (Y::*)(double))' : candidates are: X::X(const X&),
X::X(double (*)(double))

Thanks.



Relevant Pages

  • gcnew does not generate an object instance?
    ... Then the code compiles, but i get an exception when trying to address/ ... // some time later a member function is called: ...
    (microsoft.public.dotnet.languages.vc)
  • gcnew does not generate an object instance?
    ... Then the code compiles, but i get an exception when trying to address/ ... // some time later a member function is called: ...
    (microsoft.public.dotnet.general)
  • Re: C++ in ternms of C
    ... Access qualifier can be a part of a mangled member function ... you can visit private variable ... > the code compile over." ...
    (microsoft.public.vc.language)
  • Re: Verification of initialization of global objects???
    ... > A non-static member function must never be called without a completely ... > class MyClass ... > initialization. ... Just make it private. ...
    (comp.lang.cpp)
  • Re: faster access private or public?
    ... >for the compiler to throw away a private member variable... ... >5) any member function that modifies the private variable must be private ... theoretically possible for private variables to allow extra ...
    (microsoft.public.vc.language)