[comp.lang.c++] Pointers/References to Member Functions

neath@solar-1.stars.flab.Fujitsu.JUNET (05/03/89)

A recent posting to this group discussed  the use of  typedef to specify a type
that is a `pointer to a member function  of class  X'. Can  this be extended to
include  a reference to  a member function? If not,  why not? Specifically, the
following code fragment results in a "internal <<cfront 1.2.1  2/16/87>> error:
bus error" message:

	class A {
	  int a;
	public:
	  A(int n) {a = n;};
	  void print() {cout << a;};
	};
	typedef void A::MEMF;
	MEMF* p1 = A:print;                // This is accepted by cfront
	MEMF& p2 = *A:print;               // This results in bus error!

What I am really trying to do is see  if there is a  mechanism that can be used
to implement  features similar to BEFORE,  AFTER, and   AROUND methods found in
Lisp.  If I can get access  to the pointer maintaining  the link to  the member
function of a class, I can change this  to point  to some function implementing
the  appropriate method behavior. This  functionality greatly  facilitates code
reuse, since it becomes much easier to take an existing class and make a slight
modification to it without having to derive a new class from it. Any comments?

Regards
Martin Neath <neath@dsg.ti.com>
------------------------------------------------------------------------
 DISCLAIMER: As always, the opinions expressed above are strictly my own
 and do not reflect those of my employer, Texas Instruments.
------------------------------------------------------------------------
--
Regards,
Martin

mat@mole-end.UUCP (Mark A Terribile) (05/05/89)

> 	typedef void A::MEMF;
> 	MEMF* p1 = A:print;                // This is accepted by cfront
> 	MEMF& p2 = *A:print;               // This results in bus error!

Eh?  I'm surprised that cfront accepts the first line.  Can you explain to
me how this is supposed to be a valid statement in the language?

> ...  If I can get access  to the pointer maintaining the link to the member
> function of a class, I can change this to point to some function implementing
> the appropriate method behavior. This functionality greatly facilitates code
> reuse, since it becomes much easier to take an existing class and make a
> slight modification to it without having to derive a new class from it.
> Any comments?

Yeah.  If you have source code, why patch the object?  COBOL has the ALTER
statement:

		A: GO TO B.

		   . . .

		ALTER A TO C.

Rewrites the goto at A as ``GOTO C'' .

The ALTER statement is generally regarded as one of the best ways to rapidly
make code maintainance hopeless.  I can't see any difference between the
ALTER statement and what you are trying to do.  In both cases, you are
subverting the source-code-to-executable translation by making changes at
execution time.

What's wrong with deriving classes?  How does patching in at execution time
facilitate code reuse better than using the source code mechanisms that are
provided explicitly for the purpose?
-- 

(This man's opinions are his own.)
From mole-end				Mark Terribile