SomInit

This method initializes instance variables or attributes in a newly created object. Designed to be overridden. Note: The newer somDefaultInit method is suggested instead.

For backward compatibility, this method does not take an Environment parameter.


 * Original Class : SOMObject

Syntax
void somInit (SOMObject receiver)

Parameters

 * receiver (SOMObject) : A pointer to the object to be initialized.

Return Code
rc (void)

Remarks
The somInit method is invoked to cause a newly created object to initialize its instance variables or attributes.

Note: The newer somDefaultInit method performs object initialization more efficiently and is now the preferred approach for overriding initialization in an implementation file. (The somInit method still executes correctly as before.)

Because instances of SOMObject do not have any instance data, the default implementation does nothing. It is provided as a convenience to class implementors so that initialization of objects can be done in a uniform way across all classes (by overriding somInit). This method is called automatically by somNew during object creation.

A companion method, somUninit, is called whenever an object is freed. These two methods should be designed to work together, with somInit priming an object for its first use, and somUninit preparing the object for subsequent release.

If objects of your class contain instance variables or attributes, override the somInit method to initialize the instance variables or attributes when instances of the class are created. When overriding this method, always call all parent (base) classes' versions of this method beforedoing your own initialization, as follows:

1. The overriding implementation should invoke the parent method for each parent. For users of the C or C++ implementation bindings, this can be done in either of two ways: (a) by calling a _parents_ macro (which automatically invokes all parent methods) or (b) by calling the _parent_ parentName>_ macro on each parent separately.

For more information on parent method calls, see the topic "Extending the Implementation Template" in Chapter 5, "Implementing Classes in SOM," of the SOM Programming Guide.

2. The code must be written so that it can be executed multiple times without harm on the same object. This is necessary because, under multiple inheritance, parent method calls that progress up the inheritance hierarchy may encounter the same ancestor class more than once (where different inheritance paths "join" when followed backward). A check can be made to determine whether a particular invocation of somInit is the first on a given object by examining the contents of its instance variables; all the instance variables and attributes of a newly created SOM object are set to zero before somInit is invoked on that object.

More information and examples on object initialization (especially regarding the somDefaultInit method) are given in the topic "Initializing and Uninitializing Objects" in Chapter 5, "Implementing Classes in SOM," of the System Object Model Programming Guide.

Example Code
Below is the implementation for a class Animal that introduces an attribute sound of type string and overrides somInit and somUninit, along with a main program that creates and then frees an instance of class Animal. 
 * 1) define Animal_Class_Source
 * 2) include 
 * 3) include 

SOM_Scope void SOMLINK somInit (Animal somSelf) {     AnimalData *somThis = AnimalGetData (somSelf); Environment *ev = somGetGlobalEnvironment; Animal_parents_somInit (somSelf); if (!__get_sound(somSelf, ev)) { __set_sound(somSelf, ev, SOMMalloc(100)); strcpy (__get_sound(somSelf, ev), "Unknown Noise"); somPrintf ("New Animal Initialized\n"); } }

SOM_Scope void SOMLINK somUninit (Animal somSelf) {     AnimalData *somThis = AnimalGetData (somSelf); Environment *ev = somGetGlobalEnvironment; if (__get_sound(somSelf, ev)) { SOMFree(__get_sound(somSelf, ev);       __set_sound(somSelf, ev, (char*)0);        somPrintf ("Animal Uninitialized\n");        Animal_parents_somUninit (somSelf);      } }

/* main program */ #include   void main {     Animal myAnimal; myAnimal = AnimalNew ; _somFree (myAnimal); }

/* Program output: New Animal Initialized Animal Uninitialized 

Related Methods

 * somDefaultInit
 * somNew
 * somRenew
 * somDestruct
 * somUninit