开发者

Ada Finalization Adjust procedure - What to put here?

Given the following declarations :

   type Food_T is abstract tagged null record;
   type Food_Ptr is access all Food_T'Class;

   type Wrapper_T is new Ada.Finalization.Controlled with record
      Ptr : Food_Ptr;
   end record;

   procedure Adjust (Object : in out Wrappe开发者_开发百科r_T) is
   begin
      null; -- what goes here ?
   end Adjust;

I am wondering how to allocate & assign (Deep Copy) the correct derivitive of food_t when i dont know what type Object.ptr will be pointing to (and where Source & Destination are!).

Any help would be appreciated.

Thanks,

NWS.


I think you mean:

procedure Adjust (Object : in out Wrapper_T) is
begin
   Object.Ptr := new Food_T'Class'(Object.Ptr.all);
end Adjust;

Then it's Object.Ptr.all's job to ensure that it is really a deep copy, of course. (To do this, Object.Ptr.all's type might want to derive Ada.Finalization.Controlled. To allow this, you might want to make Food_T an interface so that a Food_T-deriving type can also derive from Ada.Finalization.Controlled.)


Lets say you have two instances, A and B, of the access type T. The Adjust method is then called when you do B := A.

But be careful when using this method, since it can create memory leaks when not properly used! If your idea is B to hold a full new reference to object A, then leave it empty. In that case, every pointer within B will point to the same location in memory as the pointers within A.

Just complete the method if you want to perform value type assignments, that is to say, when you want the objects to have the same "data" but in different memory locations, so that if you change A, then B will not notice it. In that case you can manually assign the values of each pointer inside the Adjust method, and create/free the internal objects if necessary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜