>How to implement and pass the pointer function in a class?
It depends on what you're trying to do with this function pointer, in most cases I would have the address of the function be passed either to the constructor as an explicit argument, or assigned with a member function to a private function pointer which the class can then use:
Code:
#include <iostream>
typedef std::ostream& (*pf)( std::ostream&, const char * );
class test
{
public:
explicit test ( pf func ) : display ( func ) {}
void print_console ( const char *mess ) { display ( std::cout, mess ); }
private:
pf display;
};
std::ostream& display_mess ( std::ostream& out, const char *mess )
{
return out<< mess;
}
int main()
{
test t ( display_mess );
t.print_console ( "This is a test" );
std::cin.get();
}
-Prelude