TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy


Options: Use Forum View

Use Proportional Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Mark Lundquist <[log in to unmask]>
Reply To:
Mark Lundquist <[log in to unmask]>
Fri, 4 Dec 1998 08:19:11 -0800
text/plain (73 lines)
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; 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;