Ryan's District
November 22, 2008, 04:10:10 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: You are not allowed to view links.
Register or Login
How to Unlock Nokia mobile Phones

You are not allowed to view links.
Register or Login
Request any TV Show / series / Episodes / movie and we will get it for you for free

 
   Home   Help Search Chat Calendar Chess Shop Login Register  
Digg This!
Pages: [1]   Go Down
  Send this topic  |  Print  
Author Topic: C++ tips - Thoughts on multiple dispatching  (Read 3213 times)
0 Members and 1 Guest are viewing this topic.
ben2ong2
Quality Poster
Paid
Hero Member
*****

Reputation: 17
Offline Offline

Gender: Male
Posts: 2374
9976.80 RD$

View Inventory
Send Money to ben2ong2

View Profile Awards
« on: October 07, 2006, 08:50:15 AM »

[JC] = jc@vivitech.com (John Cooper), Vivid Technologies Inc., 5 Aug 93

   I was wondering what the current thinking is on good approaches to
multiple dispatching in C++ programs. I was thinking of something along
these lines, but I'm interested in hearing what more experienced programmers
think:

[JS] = maxtal@physics.su.OZ.AU (John Max Skaller)

   Use a switch. There isn't any other way. Not because of some
deficiency of C++, but because its theoretically absurd. Only
single dispatch can be implemented by vectoring (indirect function call),
which is the key to object oriented techniques in static systems like C++.

[JC]

   Suppose I am writing a game that is a variant of chess in which only
certain pieces are allowed to capture certain other pieces. When a piece is
moved (say with a mouse) on the board and a player wishes to attempt a
capture, I'd like the program to do some sort of highlighting if the capture
is allowed between those two pieces.

   I had something like this in mind:

   o A base class PlayingPiece from which all the other pieces are derived.
   o Derived classes King, Queen, Bishop, Knight, Rook, Pawn.
   o Virtual functions DispatchCapture and Capture.

[JS]
   Why bother. Just write out all the cases. It wrong to use
a virtual function to handle a finite set of fixed alternatives:
thats what a switch is for.

[JC]

Example:

   void HandleCapture(PlayingPiece *attacker, PlayingPiece *defender)
   {
      :
      // Dispatch polymorphically on defender's actual type.
      defender->DispatchCapture(attacker);
      :
   }

Every derived class would have its own DispatchCapture member function:

   void King::DispatchCapture(PlayingPiece *attacker)
   {
      // Call Capture polymorphically on attacker's actual type. Which
      // overloaded Capture function gets called is based on the actual
      // type of "this".
      attacker->Capture(this);
   }

   etc.

Each derived class would override Capture as necessary:

   void King::Capture(King *defender)
   {
      /* Do whatever */
   }

   void King::Capture(Queen *defender)
   {
      /* Do whatever */
   }

   etc.

Is this a canonical technique for realizing simple multiple dispatch?

[JS]

   IMHO: this is a canonical abuse of inheritance.
And it cant possibly work :-)

Think of it this way: virtual functions implement operations
on *arbitrary* types. But a 'capture' function depends on
both the capturing and captured object piece.

For a given piece, say a Queen, there is *no way* you can possibly
specify arbitrary capturing rules in finite code. You might
specify it if you made the rules non-arbitrary, for example,
if you rated every piece with a number, and just allowed
pieces with higher ratings to capture those with lower ratings.

So: if you cant make a generalised rule that covers arbitrary
pieces, not yet dreamed up, then you should not use polymorphism
at all .. because you cant do so without violating the basic
principles which enable it in the first place: the open closed
principle.

Note: I like the example of chess pieces. Its clear that
using a common base class Piece is useful for things
like drawing the pieces. So a 'draw' routine is a good candidate
to be a pure virtual in 'Piece': this is a good place to
use polymorphism.

Note that 'draw' is a *property* of the Piece. Capture, on
the other hand is a relation. Polymorphism doesnt work with
relations. (with a couple of exceptions, as I learned
when someone gave an example that worked)

Logged

You are not allowed to view links.
Register or Login
Free Paid Survey & Home Business Resources.

You are not allowed to view links.
Register or Login
Free Article Directory | Quality Content
Ryan's District
« on: October 07, 2006, 08:50:15 AM »

 Logged
Pages: [1]   Go Up
  Send this topic  |  Print  
 
Jump to:  

Archive - WAP2 - WAP - imode
Sponsors: RAYAN.tv
-

Powered by MySQL Powered by PHP Powered by SMF 1.1.7 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 5.902 seconds with 30 queries.

Google visited last this page Today at 02:27:33 PM