comp.lang.ada
 help / color / mirror / Atom feed
* Aliasing or referencing assignment
@ 2005-09-09  0:20 David Trudgett
  2005-09-09  2:13 ` Steve
  2005-09-09 15:40 ` Jeffrey Carter
  0 siblings, 2 replies; 10+ messages in thread
From: David Trudgett @ 2005-09-09  0:20 UTC (permalink / raw)



I'm having a little difficulty with assign by reference and assign by
value (copy) semantics in a piece of code I'm working on. In the
chunk of code that follows below, I want to avoid repeating
"Self.List(Opponent_Win, Can_I_Play)" many times, and just replace it
with a variable name like "C".

"Self.List(Opponent_Win, Can_I_Play)" returns a Charles container
library Vector of Unbounded_String. The assignment:

      C := Self.List(Opponent_Win, Can_I_Play); -- "List" is 2D array

is ineffective in doing what I want because it is doing a value
copy. I've tried to turn the type definition of "C" into an access
type, but with no success. I also tried the "To_Access" function (in
charles.vectors.unbounded), but that returns a reference to an
"Element_Array" and not a Container_Type with which I can use
"Append".

Anyway, here is the code:



   procedure Initialize (Self : in out Comments)
   is
      C : String_List.Container_Type;
      package Random_Integer is new Ada.Numerics.Discrete_Random(Integer);
      use Random_Integer;
      G : Generator;
   begin

      Reset(G);
      Ada.Numerics.Float_Random.Reset(Self.PRNG, Random(G));

      -- OPPONENT WIN COMMENTS
      -- Can I Play difficulty
      C := Self.List(Opponent_Win, Can_I_Play);

      Append
        (C, To_Unbounded_String
         ("You're too good for me"));
      Append
        (C, To_Unbounded_String
         ("How did you manage that?"));

      ... and so on.

That works if I then finish with:

      Self.List(Opponent_Win, Can_I_Play) := C;


But that's copy overhead I want to avoid (fifteen times, what's
more). For the time being, I've just replaced all of the "C"s in the
Append procedure calls with "Self.List(Opponent_Win, Can_I_Play)" (and
variations thereof further down).

How can I make "C" act like a reference to the Unbounded_String Vector?

Also, is the 'To_Unbounded_String("this is a string")' really
necessary? I thought I read in the ARM that string literals were
defined for all the string types.

Thanks,

David


P.S.

Later on, I will probably read these comments from an XML file, but
for now I'm just hard coding them into the program.


-- 

David Trudgett
http://www.zeta.org.au/~wpower/

FORTH IF HONK THEN




^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2005-09-11 20:43 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-09-09  0:20 Aliasing or referencing assignment David Trudgett
2005-09-09  2:13 ` Steve
2005-09-09  7:33   ` David Trudgett
2005-09-09 15:40 ` Jeffrey Carter
2005-09-10  7:38   ` David Trudgett
2005-09-10 10:01     ` Martin Dowie
2005-09-10 10:33       ` David Trudgett
2005-09-10 11:28         ` Ludovic Brenta
2005-09-11 20:43           ` David Trudgett
2005-09-11  3:20         ` Jeffrey R. Carter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox