[comp.lang.c++] Object transformations in C++

john@hhb.UUCP (John Sissler) (06/24/89)

> How can an instantiation of one derived class transform into a
> different derived class of the same base class?  The derived classes
> have different virtual functions but have no additional private data,
> so what I really would like to do is just change the "virtual" type field.
> Below is some code that I hope illustrates the problem.

	I have had the same desire to change the virtual table in order to
	transform instances, and I suppose this is my opportunity to
	come out of the closet.

	My scenario is essentially this: during initialization of a 
	performance-critical application, a graph of instances is created. 
	After the graph is complete, each node is given a chance to 
	optimize itself based on information which is available only 
	after the graph is complete.  Each node is constrained to occupy 
	the same space.  Due to performance and memory requirements, 
	indirection (i.e. instance handles / instance pointer table) is 
	not an alternative.  Thus, one means by which the instance can 
	optimize itself is to modify its virtual table to acquire a set 
	of "fast" methods.
	
	A cfront-dependent method of achieving this is
	to simply modify the vptr field, for example:

	void Derived::Transform(const AnotherDerived& newInstance) {
		_vptr = newInstance._vptr;
	}

	Admittedly, I have only experimented with this technique, and
	I doubt whether I'll actually use it in production code unless
	it gets language support (which I doubt it will or should).