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
Igor Izvarin <[log in to unmask]>
Reply To:
Fri, 4 Dec 1998 19:02:54 +0300
text/plain (91 lines)
Thank you.

Yes, I knew that it is possible to do with several packages. But it is not
an elegant solution because I have up to ten functions and each has it own
static variables. So I need to make one package for every function and one
package for every list of static variables?? Too complicated. May be someone
could offer better solution.

Again, thank you very much for your help.

-----Original Message-----
From:   Mark Lundquist [mailto:[log in to unmask]]
Sent:   Friday, December 04, 1998 7:19 PM
To:     [log in to unmask]
Cc:     [log in to unmask]
Subject:        Re: Static variables in Ada

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;