Object+Oriented+Programming+with+ANSI+C+02

Object+Oriented+Programming+with+ANSI+C+02 - 103 _ _ 9...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
103 ___________________________________________________________________________ 9 Static Construction Self-Organization 9.1 Initialization Class descriptions are long-lived objects. They are constant and they exist practi- cally as long as an application executes. If possible, such objects are initialized at compile time. However, we have decided in chapter 6 that static initialization makes class descriptions hard to maintain: the order of the structure components must agree with all the initializations, and inheritance would force us to reveal dynamically linked methods outside their implementation files. For bootstrapping we initialize only the class descriptions Object and Class at compile time as static structures in the file Object.dc . All other class descriptions are generated dynamically and the metaclass constructors beginning with Class_ctor() take care of inheritance and overwriting dynamically linked methods. ooc generates initialization functions to hide the details of calling new() to gen- erate class descriptions, but the fact that they must be explicitly called in the appli- cation code is a source of hard to diagnose errors. As an example, consider init- Point() and initCircle() from section 6.10: void initPoint (void) { if (! PointClass) PointClass = new(Class, "PointClass", Class, sizeof(struct PointClass), ctor, PointClass_ctor, 0); if (! Point) Point = new(PointClass, "Point", Object, sizeof(struct Point), ctor, Point_ctor, draw, Point_draw, 0); } The function is designed to do its work only once, i.e., even if it is called repeatedly it will generate a single instance of each class description. void initCircle (void) { if (! Circle) { initPoint(); Circle = new(PointClass, "Circle", Point, sizeof(struct Circle), ctor, Circle_ctor, draw, Circle_draw, 0); } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
104 ___________________________________________________________________________ 9 Static Construction — Self-Organization Both functions implicitly observe the class hierarchy: initPoint() makes sure that PointClass exists before it uses it to generate the description Point ; the call to init- Point() in initCircle() guarantees that the superclass description Point and its meta- class description PointClass exist before we use them to generate the description Circle . There is no danger of recursion: initCircle() calls initPoint() because Point is the superclass of Circle but initPoint() will not refer to initCircle() because ooc does not permit cycles in the superclass relationship. Things go horribly wrong, however, if we ever forget to initialize a class descrip- tion before we use it. Therefore, in this chapter we look at mechanisms which automatically prevent this problem. 9.2 Initializer Lists — munch Class descriptions essentially are static objects. They ought to exist as long as the main program is active. This is normally accomplished by creating such objects as global or static variables and initializing them at compile time. Our problem is that we need to call Class_ctor() and the other metaclass con- structors to hide the details of inheritance when initializing a class description.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 08/10/2011.

Page1 / 118

Object+Oriented+Programming+with+ANSI+C+02 - 103 _ _ 9...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online