ModuleCopy - Maple Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Programming : Objects : Commands Used with Objects : ModuleCopy

ModuleCopy

specify how an object is copied by Object

 

Calling Sequence

Description

Examples

Compatibility

Calling Sequence

module() option object; export ModuleCopy, ...; ... end module;

Description

• 

If an object defines a method named ModuleCopy, that method will be called when a new object of the same class is created via a call to Object.  

• 

The main reasons to implement a ModuleCopy routine are:

– 

to initialize the data members of a newly created object with values different from the data members of the prototype object (potentially based on arguments passed to the Object routine)

– 

to perform a deep copy of data members (for example, creating a copy of an rtable member instead of both objects referencing the same rtable)

• 

The ModuleCopy function is only useful for modules that are objects (that is, declared with option object).

• 

The calling sequence of a ModuleCopy routine is as follows:

ModuleCopy::static := proc( new::Object, proto::Object, ... )

  

new is the newly created object that is being initialized.  proto is the prototype object that was passed into the call to Object.  Any remaining arguments are the extra arguments passed into the Object routine.

• 

Implementing a ModuleApply routine that calls Object to invoke ModuleCopy makes applying the prototype object into an object factory.

Examples

module Obj1()
   option object;
   local data := 0;

   export getData::static := proc( self::Obj1 )
       self:-data;
   end;

   export ModuleCopy::static := proc( self::Obj1, proto::Obj1, d, $ )
       if ( _npassed = 2 ) then
           self:-data := proto:-data;
       else
           self:-data := d;
       end;
   end;
end:

getDataObj1

0

(1)

newObj1ObjectObj1

newObj1:=Object<<Obj1,140227545668928>>

(2)

getDatanewObj1

0

(3)

newObj2ObjectObj1&comma;10

newObj2:=Object<<Obj1,140227545676576>>

(4)

getDatanewObj2

10

(5)

module Obj2()
   option object;
   local data := 0;

   export getData::static := proc( self::Obj2 )
       self:-data;
   end;

   export ModuleApply::static := proc( )
       Object( Obj2, _passed );
   end;

   export ModuleCopy::static := proc( self::Obj2, proto::Obj2, d, $ )
       if ( _npassed = 2 ) then
           self:-data := proto:-data;
       else
           self:-data := d;
       end;
   end;
end:

newObj1Obj2

newObj1:=Object<<Obj2,140227545689024>>

(6)

getDatanewObj1

0

(7)

newObj2Obj210

newObj2:=Object<<Obj2,140227545697056>>

(8)

getDatanewObj2

10

(9)

Compatibility

• 

The ModuleCopy command was introduced in Maple 16.

• 

For more information on Maple 16 changes, see Updates in Maple 16.

See Also

module

ModuleApply

Object

Object,create

Object,overview

procedure

rtable

 


Download Help Document

Was this information helpful?



Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam