On this page:
The Slice compiler generates the following code for this definition:
There are a number of things to note about the generated code:
- The generated
TimeOfDayclass inherits from
Ice.Value. Note that
Ice.Valueis not the same as
Ice.ObjectPrx. In other words, you cannot pass a class where a proxy is expected and vice versa.
- The generated class provides a constructor that accepts a value for each data member.
- The generated class defines a property for each Slice data member.
There is quite a bit to discuss here, so we will look at each item in turn.
As for Slice interfaces, the generated type for a Slice class implicitly inherits from a common base type. However, the type inherits from
Ice.Value instead of
Ice.ObjectPrx (which is at the base of the inheritance hierarchy for proxies). As a result, you cannot pass a class where a proxy is expected (and vice versa) because the base types for classes and proxies are not compatible.
Ice.Value defines a number of member functions:
The member functions of
Ice.Value behave as follows:
This function returns the static type ID of a class.
This function returns the actual run-time type ID for a class. If you call
ice_idthrough a reference to a base instance, the returned type ID is the actual (possibly more derived) type ID of the instance.
The Ice run time invokes this function prior to marshaling the object's state, providing the opportunity for a subtype to validate its declared data members.
The Ice run time invokes this function after unmarshaling an object's state. A subtype typically overrides this function when it needs to perform additional initialization using the values of its declared data members.
This functions returns the
SlicedDataobject if the value has been sliced during un-marshaling or
The type generated for a Slice class provides a constructor that initializes each data member to a default value appropriate for its type:
|Data Member Type||Default Value|
|First enumerator in enumeration|
If you wish to ensure that data members of primitive and enumerated types are initialized to specific values, you can declare default values in your Slice definition. The constructor initializes each of these data members to its declared value instead.
The constructor accepts one argument for each member of the class. This allows you to create and initialize an instance in a single statement, for example:
For derived classes, the constructor requires an argument for every member of the class, including inherited members. For example, consider the the definition from Class Inheritance once more:
The constructors generated for these classes are similar to the following:
undefined as the value of any optional data member that you wish to remain unset.
By default, data members of classes are mapped exactly as for structures and exceptions: for each data member in the Slice definition, the generated type defines a corresponding property.
Optional data members use the same mapping as required data members, but an optional data member can also be set to
undefined to indicate that the member is unset. A well-behaved program must compare an optional data member to
undefined before using the member's value:
<class-name>Disp class, which can be used to implement an Ice object with these operations. For example:
While value factories are necessary when using classes with operations (a now deprecated feature), value factories may be used for any kind of class and are not deprecated.
The Ice run time will by default create and return a plain
If you wish, you can create your own custom derived class, and tell Ice to create and return these instances instead. For example:
Now, whenever the Ice run time needs to instantiate an object with the type ID
"::M::TimeOfDay", it calls the registered factory, which returns a
TimeOfDayI instance to the Ice run time.