SomDefaultCopyInit: Difference between revisions
No edit summary |
|||
Line 54: | Line 54: | ||
* [[somDefaultConstAssign]] | * [[somDefaultConstAssign]] | ||
[[Category: | [[Category:SOMObject]] |
Latest revision as of 03:55, 13 October 2017
This method provides support for call-by-value object parameters in methods introduced by DTS C++ classes. May be overridden, but, if appropriate, somDefaultConstCopyInit should be overridden instead.
- Original Class
- SOMObject
Syntax
void somDefaultCopyInit (SOMObject receiver, somInitCtrl ctrl, SOMObject fromObj)
Parameters
- receiver (SOMObject)
- A pointer to an uninitialized object of an arbitrary SOM class, S.
- ctrl (somInitCtrl)
- A pointer to a somInitCtrl structure, or NULL.
- fromObj (SOMObject)
- A pointer to an object of class S or some class descended from S.
Return Code
rc (void)
Remarks
The somDefaultCopyInit method would be called a "copy constructor" in C++. In SOM, this concept is supported using an object initializer that accepts the object to be copied as an argument. Copy constructors are used in C++ to pass objects by value. They initialize one object by making it be a copy of another object. In SOM, objects are always passed by reference, so arguments to DTS C++ methods that receive call-by-value object parameters are actually passed by reference. But, to correctly support the semantics of DTS C++ call-by-value arguments, it is necessary to actually pass a copy of the intended argument. In general, somDefaultCopyInit should be used to make this copy.
The default behavior provided by somDefaultCopyInit is to do a shallow copy of each ancestor class's introduced instance variables. However, a class may always override this default behavior (for example, to do a deep copy for certain variables). If it is possible to avoid modification of fromObj when doing the copy, the method somDefaultConstCopyInit should be overridden for this purpose. Only if this is not possible (and shallow copy is not appropriate) would it be appropriate to override somDefaultCopyInit.
The considerations important to overriding somDefaultCopyInit are similar to those described in the SOM Programming Guide for overriding somDefaultInit. (See "Initializing and Uninitializing Objects" in Chapter 5, "Implementing Classes in SOM.") The basic difference between somDefaultInit and somDefaultCopyInit is that the latter method takes an object (fromObj) as an argument that is to be copied.
Example Code
// IDL produced by a DTS C++ compiler for a DTS C++ class interface X : SOMObject { void foo(in SOMClass arg); implementation { foo: cxxdecl = "void foo(SOMClass arg)"; // !! call-by-value }; }; // C++ SOMObjects Toolkit Code #include <X.xh> #include <somcls.xh> main() { X *x = new X; SOMClass *arg = _SOMClass->somNewNoInit(); // make arg be a copy of the X class object arg->somDefaultCopyInit(0,_X); x->foo(arg); // call foo with the copy }