45

How to declare a pointer to a member function?

Ans: Suppose, I wish to declare a pointer to a member function that receives an int and returns an int. I will have to declare it as int ( A::* ) ( int ). Following is an example.

#include

class A

{

public :

int fun ( int f )

{

cout << "in fun\n" ;

return f * f ;

}

} ;

typedef int ( A:: *pfun ) ( int ) ;

void main( )

{

pfun p = A::fun ;

A a ;

int s = ( a.*p ) ( 6 ) ;

cout <<>

}

It's very calm over here, why not leave a comment?

Leave a Reply




Feed Feed
Feed Feed