[comp.lang.c++] swapping virtual function table pointers

vaughan@puma.cad.mcc.com (Paul Vaughan) (08/22/89)

	I occasionally have related classes with identical data and
function members but differing virtual function definitions.  I'd like
to be able to change the behavior of objects of such classes by
changing the virtual function table pointer in the existing objects.
I use g++ 1.35.1 at the moment.  Three questions:

	1) Is this feasible in cfront 2.0 and/or g++?
	2) Is it possible to define in a portable way within the language?
	3) Could anyone share a hack to do this?

	Now for something more complex, assuming I'm not already
totally fouled up.  Suppose I copied a virtual function table for some
class and changed the virtual function table pointer of an instance of
that class to point to the copy.  Now suppose I were to change
individual elements of this copied table in order to dynamically
change the behavior of an object.

	1) Has anybody tried this?
	2) Does it sound feasible?


 Paul Vaughan, MCC CAD Program | ARPA: vaughan@mcc.com | Phone: [512] 338-3639
 Box 200195, Austin, TX 78720  | UUCP: ...!cs.utexas.edu!milano!cadillac!vaughan

ark@alice.UUCP (Andrew Koenig) (08/22/89)

In article <2438@cadillac.CAD.MCC.COM>, vaughan@puma.cad.mcc.com (Paul Vaughan) writes:

> 	I occasionally have related classes with identical data and
> function members but differing virtual function definitions.  I'd like
> to be able to change the behavior of objects of such classes by
> changing the virtual function table pointer in the existing objects.

C++ does not support that operation.  Objects do not have mutable types.
If you try it, you get what you get.  Sorry about that.

Consider, for instance, a C++ implementation that distinguishes between
objects of different types by reserving a separate block of (virtual)
memory for each type and using the address of the object to figure
out how to resolve virtual function calls.  Such an implementation
would not have any virtual function tables at all, which would make
it hard to change it.

You may be able to achieve a similar effect in some C++ implementations
by various kinds of pointer cheating.  There is no guarantee that
any such hack will continue to work, and I'm reluctant even to give
advice as to how to do it.
-- 
				--Andrew Koenig
				  ark@europa.att.com