{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

Info icon This 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); } }
Image of page 1

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

View Full Document Right 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.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern