TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy

TEAM-ADA@LISTSERV.ACM.ORG

Options: Use Forum View

Use Monospaced 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
Subject:
From:
Mark Lundquist <[log in to unmask]>
Reply To:
Mark Lundquist <[log in to unmask]>
Date:
Fri, 4 Dec 1998 08:19:11 -0800
Content-Type:
text/plain
Parts/Attachments:
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;

...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;

ATOM RSS1 RSS2