From: Igor Izvarin <[log in to unmask]>
> Hello everybody.
>
> I am novice in Ada programming and just came from C/C++. Sorry I could not
> find another discussion forum on Internet more technical so I am asking
> question here.
>
> I translate my source code from C/C++ and meet one feature I can not
> resolve: the static variables in Ada.
>
> The example:
>
> In C:
>
> Int foo()
> {
> static int v;
> v = init_static();
> ...
> }
>
> and the v variable will hold the value between calls. How can I do this in
> Ada?
>
Is that a good example? (the value of v is assigned again anyway by
the call to init_static() ...?)
In any case... I think
package body Outer is
package Inner is
function Foo return Integer;
end Inner;
package body Inner is
V : Integer; -- V is visible within the body of
-- Inner, e.g. to Foo
function Foo return Integer is
.
.
.
end Inner;
function Foo renames Inner.Foo;
end Outer;
...gives you what you want. Here, Inner exists just to hide V. If you
added more things to Inner, they could also see V; this would then be
wider visibility than a function static variable.
If you don't care about hiding V to other things in the body of Outer,
you can dispense with Inner (and the rename-as-body) altogether:
package body Outer is
V : Integer;
function Foo return Integer is
.
.
.
end Inner;
end Outer;