From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
Subject: Re: Criticisms Wanted:Callbacks&Runtime methods
Keywords: callbacks C++ Runtime methods
Organization: MAXTAL P/L C/- University Computing Centre, Sydney
Date: Sat, 12 Dec 1992 15:40:30 GMT

In article <1gc6fhINN6nr@tamsun.tamu.edu> pinky@tamu.edu (The Man Behind The Curtain) writes:
>
>A few days ago, I posted asking how one could model calling a method
>of an arbitary object without knowing whether or not that method
>existed for that object.  Basically, I wanted to know if one could
>have a runtime determination of an object's methods.
>
>But these capabilities are useful, and even necessary when you try
>to model the real world.  For instance, might double click a key, and
>then select from the screen any object to try to unlock with the key,
>be it a bear, bush, or door.  These classes are mostly unrelated, but
>in order to be able even attempt the operation, the base class of such
>objects must at least have method for unlocking.  If objects can
>interact in a wide variety of ways (a large number of verbs), then
>the base class would contain an unweildy number of virtual functions.

	So put in all those methods! It is not expensive per object,
only per class. I know exactly what you are talking about, having
spent some months preparing a quote for a D&D game. There is
a simple set of properties, but the objects all have different
combinations of them. The interactions of different weapons,
wielded by different characters against different classes of
monsters is complex, and it cannot be easily generalised.

	One approach is to emulate the dynamism that C++ lacks.
For while C++ is essentially static, it provides the low level
tools to implement much more powerful systems.

	For example, you can suuply to each object a linked
list of attributes: asking if an object has the attribute
'can be unlocked' is then simple to implement (scan the list
for the attribute name 'unlockable'.

	The static alternative is to make all objects
unlockable, except some have probability of sucess 0 with
any key.

	The third approach is to use Smalltalk :-)

You will note that your problem cannot be solved 'properly' no matter
what you do, since it requires nonexistant multimethods, that is, the effect
of putting some key in some door cannot in general be computed
with virtual functions: it depends on the exact type of the two objects.
The only way to do it in general is with a very large lookup table.
To avoid this, simplifiying assumptions are required: some general code
and some special cases.

	For example, all swords work the same way, they just have
different probabilities of hitting, penetration and damage.
Multiplication and addition of numbers is the most general
mechanism I can think of, along with a few special cases.

	(Whenever a Dragon Slayer is used on a Dragon, triple
damage results and the dragon loses er fire for two rounds,
provided the damage was to the head .... )


-- 
;----------------------------------------------------------------------
        JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
	Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
Newsgroups: comp.lang.c++