Encapsulation, Extension, and Function Dispatch in C++

C. Benjamin Flynn & David G. Wonnacott


ABSTRACT

	This paper will discuss a means by hierarchical class structures in
C++ may be extended without violating class encapsulation.  The idea is to
create a syntactically and abstractly pleasant means of adding on to an
existing class, a means that makes sense and does not alter any
functionality of the class already in place.

	Functions relating to classes may be placed into three basic
categories.  The first two are the author's -- functions essential to the
class and functions useful to the class.  The third is the product of later
users providing useful functions.  The essential functions are those which
provide the basic functionality of the class and are the only ones with
privileges to its protected data.  The author's useful functions use the
essential functions to provide tools for working with the class.  Useful
functions written by others also use the essential functions to provide
tools, but have certain restrictions on their implementation.

	These distinctions enable us to better understand the concerns of
class authorship and extension.  Separating the author's functions into
essential and useful simplifies reasoning about the class.  Our class
abstraction need only be based on the essential functions of the class;
proving the proper functionality of the class may be done by proving it to
work within the essential functions.  The restrictions on our third
category of functions arises from the amount of source code the respective
authors have access to.  The author of the class has access to its
complete source code, while subsequent authors may have access only to the
abstraction or header.  These subsequent authors wanting to extend the
class experience a dilemma under the present system.  If the functions are
added outside the classes they cannot be virtual, and the type of an
object pointer, rather than the type of the object pointed to, will be
used for function selection.

	We propose to resolve this dilemma by creating a new class of
functions that we call "accessory functions."  Accessory functions are
declared outside the class, and thus have no privilege to protected data
of the class, but are also dynamically dispatched, like virtual functions.
This new system provides a convenient means for making type information
available during such traversals.  This paper defines the syntax and
semantics of accessory functions, and outlines one possible
implementation.