Creating anonymous instances

You can create anonymous instances, apply the C++ new operator as in any conventional C++ program. IBM® Rational® Rhapsody® generates a default constructor (ctor) for every class in the system. You can add additional constructors using the browser.

About this task

For example, to create an instance of class A using the default constructor, enter the following code:

   A *a = new A();

For reactive and composite classes, the default constructor takes a thread parameter that, by default, is set to the main thread for the system. To associate the instance with a specific thread rather than the main thread for the system, you must explicitly pass this parameter to the constructor.

The following example creates an instance of class A and assigns it to a thread T.

   A *a = new A(T);

After creating an instance, you would probably call its relation mutators to connect it to its peers (see Using relations). If the class is reactive, you would probably call its startBehavior() method next.

Composite instances manage the creation of components by providing dedicated operations for the creation of new components. For each component, there is an operation phil of type Philosopher. The new phil operation creates an instance, adds it to the component relation, and passes the thread to the new component.

The following code shows how the composite sys can create a component phil.

Philosopher *pPhil = sys->newPhil();

After creating the instance, you would probably call its relation mutators to connect it to its peers. If the class is reactive, you would probably call its startBehavior() method next.


Feedback