C++ and Toolbox
Volume Number: 10
Issue Number: 2
Column Tag: Think Top 10
Mixing C++ and the Toolbox 
By Christopher Prinos, Symantec Technical Support, Symantec Corp.
Note: Source code files accompanying article are located on MacTech CD-ROM orsource code disks.
This is a monthly column written by Symantec’s Technical Support Engineers
intended to provide you with information on Symantec products. Each month we cover
either a specific application of tools or a “Q&A” list.
Mixing C++ and the Toolbox
As the use of C++ continues to grow in the Macintosh community, programmers
are faced with the challenge of learning ways to bring the added capabilities of the C++
language to their Macintosh development projects. A large number of people that
contact us everyday are just getting their hands wet in the Toolbox, even more are just
starting to learn C++. Bringing the two together is commonly described in the context
of class libraries like the THINK Class Library and MacApp, and although these
libraries provide powerful vehicles to develop object-oriented Macintosh
applications, they may be a bit overwhelming at first. Those coming up to speed in
C++ are often looking a for nudge in the right direction to start their Mac specific
projects, without having to assimilate a large class library with its possibly subtle
interaction between a large number of classes.
This article will take a more straightforward approach. We will build a C++
class using features like operator overloading, nested classes, and reference counting
to implement a wrapper for QuickDraw regions that allows us to use them in a more
intuitive and elegant manner. The final result will be a stand-alone class that doesn’t
rely on a supporting cast of other classes to be effective, showing a concrete example of
integrating some of the power of the C++ language with the Macintosh Toolbox.
What you should know
This article assumes that you have a basic understanding of QuickDraw in
general, and QuickDraw regions in particular. You should be familiar with the way
regions are created, and the basic operations that can be performed on them. If you
need a bit of a refresher, look to Inside Macintosh Vol. 1, Chap 6., or the New Inside
Macintosh: Overview, Chap. 5. You should also have a familiarity with C++ concepts
such as class declarations, constructors and destructors, and overloaded operators. In
general, I’ll assume the reader has done some Macintosh programming in another
language (such as C or Pascal), but is getting started in C++.
Software Requirements
The code for this was written and tested using Symantec C++ for Macintosh,
version 6.0.1. It will not compile using THINK C with object extensions. The code is
also suitable for the MPW version of Symantec C++ without modification.
What’s the idea?
A major advantage of C++ (and object programming in general) is that it
provides language supported constructs for the programmer to bind data (structures)
and the operations (functions) that operate on that data. Using other features like
inheritance and polymorphism allows objects to be extended and combined in powerful
ways, but it is the underlying data-operation binding that enables objects to be used as
“black boxes” in a high level, reusable way.
Defining a data structure and the operations that can be performed on it is often
referred to as an Abstract Data Type (ADT). A common example is that of a stack. The
data are the items in the stack, and the operations could be push, pop, and empty. How
the data and operations are actually implemented can vary, but the concept is that of a
set of data, and a set of actions. In a procedural language, that set of data and actions
remains separated, but in C++ (and other object oriented languages), they are part of
the same object.
The QuickDraw Region as an Abstract Data Type
The QuickDraw region is an excellent example of a procedural implementation of
an ADT. There is a single data structure, a Region which is passed around from
function to function using a handle to the structure, typedef’ed as a RgnHandle. Regions
can be used to describe arbitrarily complex graphic outlines and areas, although they
often describe simple rectangles. QuickDraw provides operations such as UnionRgn and
XorRgn to manipulate the data in a region. The functions available are certainly
complete, but can be unwieldy. Consider the following (contrived) code:
/* 1 */
RgnHandle a,b,c;
Rect rect1;
PicHandle picH;
// ... assume above variables are defined here
RgnHandle d,e,f;
d = NewRgn();
e = NewRgn();
DiffRgn(b,c,d);
UnionRgn(a,c,e);
if ( EmptyRgn(d) || EqualRgn(e,b) )