comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <mheaney@on2.com>
Subject: Re: STATIC types in ADA?
Date: Fri, 1 Mar 2002 10:33:58 -0500
Date: 2002-03-01T10:33:58-05:00	[thread overview]
Message-ID: <u7v7h34o2ms5ac@corp.supernews.com> (raw)
In-Reply-To: 3c7f8fd6$0$7055$43695faf@reader


<jhuber@nlxcorp.ellsworth.com> wrote in message
news:3c7f8fd6$0$7055$43695faf@reader...
> Do STATIC data types (like C++ has) exist in ADA?
>
> By this I mean, is there a way I can define a variable so that
> when I call a procedure and set a value (say 5.0) and then
> exit the procedure and come back into it at a later time, the
> variable would still have the same value (i.e., 5.0)?

Just declare the object in a static scope (a package):

package P is
   F : Float := 0.0
end;

with P;
procedure Op is
begin
   P.F := 5.0;
end;

Realize that a variable declared in a package is equivalent to an object
declared in a C++ namespace, or as a static member of a class:

//n.hpp
namespace N
{
   extern float f;
}

//n.cpp
float N::f;

or

//c.hpp
class C
{
public:
   static float f;
//...
};

//c.cpp
float C::f;


Often variables are hidden in the body of the package, so you could write it
as:

package P is
   procedure Op;
end;

package body P is
   F : Float := 0.0;

   procedure Op is
   begin
      F := 5.0;
   end;
end P;

That would translate as:

//n.hpp
namespace N
{
   void op();
}

//n.cpp
namespace
{
   float f;
}

void N::op()
{
   f = 5;
}


or

//c.hpp
class C
{
public:
   static void op();
//...
private:
   static float f;
//...
};

//c.cpp
float C::f;

void C::op()
{
   f = 5;
}

Actually, you could declare f in an anonymous namespace in the
implementation file c.cpp, instead of as a static variable of the class.
This lets you hide it completely.  One of my complaints about C++ is that
you often have to put things in the private part of the class declaration,
up in the header file, instead of in the implementation file.  Thus in order
to get visibility, I pay with a compilation dependency.  I haven't found a
satisfactory way around this.







  parent reply	other threads:[~2002-03-01 15:33 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-03-01 14:27 STATIC types in ADA? jhuber
2002-03-01 15:06 ` Jim Rogers
2002-03-01 15:33 ` Matthew Heaney [this message]
2002-03-01 17:51 ` Jeffrey Carter
  -- strict thread matches above, loose matches on Subject: below --
2002-03-01 20:42 Gautier Write-only-address
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox