HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, a dispatch table is a table of pointers or
memory address In computing, a memory address is a reference to a specific memory location used at various levels by software and hardware. Memory addresses are fixed-length sequences of digits conventionally displayed and manipulated as unsigned integers. Su ...
es to functions or
method Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to: *Scien ...
s. Use of such a table is a common technique when implementing
late binding In computing, late binding or dynamic linkage—though not an identical process to Dynamic linker, dynamically linking imported code Library (computing), libraries—is a computer programming mechanism in which the Method (computer programming), ...
in
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pr ...
.


Perl implementation

The following shows one way to implement a dispatch table in
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offici ...
, using a hash to store references to code (also known as
function pointer A function pointer, also called a subroutine pointer or procedure pointer, is a pointer that points to a function. As opposed to referencing a data value, a function pointer points to executable code within memory. Dereferencing the function poin ...
s). # Define the table using one anonymous code-ref and one named code-ref my %dispatch = ( "-h" => sub , "-g" => \&say_goodbye ); sub say_goodbye # Fetch the code ref from the table, and invoke it my $sub = $dispatch; print $sub ? $sub->() : "unknown argument\n"; Running this Perl program as perl greet -h will produce "hello", and running it as perl greet -g will produce "goodbye".


JavaScript implementation

Following is a demo of implementing dispatch table in JavaScript: var thingsWeCanDo = ; var doSomething = function(doWhat)


Virtual method tables

In
object-oriented Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pro ...
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s that support
virtual method In object-oriented programming, in languages such as C++, and Object Pascal, a virtual function or virtual method is an inheritable and overridable function or method for which dynamic dispatch is facilitated. This concept is an important part o ...
s, the
compiler In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs that ...
will automatically create a dispatch table for each object of a
class Class or The Class may refer to: Common uses not otherwise categorized * Class (biology), a taxonomic rank * Class (knowledge representation), a collection of individuals or objects * Class (philosophy), an analytical concept used differentl ...
containing virtual methods. This table is called a
virtual method table In computer programming, a virtual method table (VMT), virtual function table, virtual call table, dispatch table, vtable, or vftable is a mechanism used in a programming language to support dynamic dispatch (or run-time method binding). When ...
or ''vtable'', and every call to a virtual method is dispatched through the vtable.


See also

*
Branch table In computer programming, a branch table or jump table is a method of transferring program control (Branch (computer science), branching) to another part of a program (or a different program that may have been dynamically loaded) using a table of b ...


References

*
Diomidis Spinellis Diomidis D. Spinellis ( el, Διομήδης Δ. Σπινέλλης; 2 February 1967, Athens) is a Greece, Greek computer science academic and author of the books ''Code Reading'', ''Code Quality'', ''Beautiful Architecture'' (co-author) and ''Effe ...
(2003). ''
Code Reading ''Code Reading'' ({{ISBN, 0-201-79940-5) is a 2003 software development book written by Diomidis Spinellis. The book is directed to programmers who want to improve their code reading abilities. It discusses specific techniques for reading code wri ...
: The Open Source Perspective''. Boston, MA: Addison-Wesley. {{ISBN, 0-201-79940-5 Method (computer programming) Articles with example Perl code