Subject: | |
From: | |
Reply To: | Robert I. Eachus |
Date: | Fri, 4 Dec 1998 15:51:28 -0500 |
Content-Type: | text/plain |
Parts/Attachments: |
|
|
At 05:48 PM 12/4/98 +0300, Igor Izvarin wrote:
>I translate my source code from C/C++ and meet one feature I can not
>resolve: the static variables in Ada.
Most of the responces have explained how to do this using variables in
library packages, because that is the way it is almost always done in
Ada--a set of variables and operations associated with them are grouped
into a package. But there are several other techniques for reaching the
same goal:
First, if tasking is a concern, you can have a protected object which
has an integer state:
protected Guarded is
procedure Set(V: in Integer);
function Get return Integer;
-- possibly other operations
private
V: Integer := Init_Static;
end Guarded_Integer;
Note that because of the rules for protected object, Set can change the
value of V, and Get can be called from multiple threads simultaneously, but
can't change V.
There are many other tasking variations.
Or you can make your function a generic function with a formal object
parameter:
generic
V: in out Integer;
function Foo return Integer;
function Foo return Integer is...end Foo;
Now whenever you instantiate Foo you have to pass it a parameter which is
an Integer variable. Foo can change the value of the variable, but doesn't
have to.
My_V: Integer := Init_Static;
function My_Foo is new Foo(My_V);
This is not very comfortable, but if V is only set once, at the point
where the generic is declared, then you can just do:
generic
V: in Integer;
function Foo return Integer;
...
function My_Foo is new Foo(Init_Static);
If you want you can even use this idiom where V changes, by making the
generic parameter an parameter, or of an access type.
Robert I. Eachus
with Standard_Disclaimer;
use Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...
|
|
|