In addition to what CPallini said the type of function pointer you want to store the address of the member function in is wrong - without a fairly heavyweight cast (i.e. a
reinterpret_cast
- the mallet of all casts) you'll never get that assignment to work. And even if you did it'd just crash and burn messily.
It's a fairly common idiom to store a pointer to an object and the operation to be carried out on the object in another object. I know you said you didn't want to use a delegate, but, that's the sort of thing you're going to have to do:
class B;
class A
{
public:
A( B *object, void (B::*function)( int ) )
: object_( object ), function_( function ){}
void do_something( int n )
{
if( object_ && function_ ) (object_->*function_)( n );
}
private:
void (B::*function_)( int );
B *object_;
};
class B
{
public:
void do_something( int n )
{
std::cout << n << std::endl;
}
};
int main()
try
{
B b;
A a( &b, &B::do_something );
a.do_something( 27 );
}
catch( std::exception &e )
{
std::cout << "Something went wrong: " << e.what() << std::endl;
}
catch(...)
{
std::cout << "Something went wrong, no idea what!" << std::endl;
}
This gives you two axes of variation for customising A. As an example this works really well for bundling up abstract factories into "ordinary" factory methods.
Perhaps if you gave us a bit more information about what you want to do (i.e. what the problem is you're trying to solve, not the solution you're trying) someone might be able to suggest something that does the same thing in idiomatic C++.