[comp.lang.c++] Casting pointer to member functions.

darrenp@dibbler.cs.monash.edu.au (Darren Platt) (06/20/91)

I have read with interest the discussion on obtaining and calling pointer
to member functions. I am being forced by a lecturer who still thinks 
FORT**N and gotos are a wonderful idea, to write a simulation system.

I have a collection of different objects in the simulation all derived
from a base class called process. I need to be able to put events on
a queue which basically consist of a pointer to a member function.

For example,

class process {
	// Generic process type in simulation
}

class fred : public process {
	public:
		void fred_gets_up(void);
		void fred_goes_to_work(void);
		void fred_goes_home(void);
}

By storing a pointer to process and a pointer to member of process pointer
I can call any function in process:

process  *process_ptr;
process::* ptr_to_memb;

(process_ptr.*ptr_to_memb)();

Unfortunately, I don't seem to be able to call a member function in a
class derived from process in the same way since they all have different
types of pointer to member pointers (ie fred::* mary::* etc).

At the moment, I am calling a virtual function in process which invokes
a function in the specific object (fred for example). It is the specific
object's job to decide which stage it is up to. It would be much nicer
if the event scheduler could call the right member function directly
(eg fred_goes_to_work).

Is this possible ?, Am I missing something obvious ?

Comments would be appreciated.

Darren Platt.
darrenp@dibbler.cs.monash.edu.au
(Clayton, Melbourne, Australia)