For Programmers: Free Programming Magazines  


Home > Archive > Fortran > April 2005 > Quick question about PRIVATE and PUBLIC









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Quick question about PRIVATE and PUBLIC
Neilen Marais

2005-04-22, 3:59 pm

hi!

If I want to define some public, and some private variables in f90,
can I do the following:

PUBLIC

! Define several public variables
....

PRIVATE

!Define several private variables
....

Or is the public/private thing global? IOW, if I say PRIVATE, I have
to define each public variable as such individually?

Thanks
Neilen

--
you know its kind of tragic
we live in the new world
but we've lost the magic
-- Battery 9 (www.battery9.co.za)
Ian Bush

2005-04-22, 3:59 pm


Hi Neilen,

Neilen Marais wrote:

> hi!
>
> If I want to define some public, and some private variables in f90,
> can I do the following:
>
> PUBLIC
>
> ! Define several public variables
> ...
>
> PRIVATE
>
> !Define several private variables
> ...
>


No

> Or is the public/private thing global? IOW, if I say PRIVATE, I have
> to define each public variable as such individually?
>


Yes, this is the case. By default all variables at the top of the module
(don't know the technical term) are public. Sticking in a private statement
changes that for all such vars, unless they are explicitly declared public,

Hope this helps,

Ian


Tom Micevski

2005-04-22, 3:59 pm

Neilen Marais wrote:
> hi!
>
> If I want to define some public, and some private variables in f90,
> can I do the following:
>
> PUBLIC
>
> ! Define several public variables
> ...
>
> PRIVATE
>
> !Define several private variables
> ...
>
> Or is the public/private thing global? IOW, if I say PRIVATE, I have
> to define each public variable as such individually?


yes, if you say PRIVATE first, then you must name the PUBLIC variables
(and vice versa), eg.

module mod
private
public :: a, b, c
integer :: a, b, c, d, e, f
contains
function func
....
end function func
end module mod

so, when USEing this module:
USE mod
only variables a, b, c would be visible/useable (and d, e, f, and func
would be "hidden").
Neilen Marais

2005-04-22, 3:59 pm

Hi

Thanks for the info Ian and Tom.

On Fri, 22 Apr 2005 13:28:22 +0000, Ian Bush wrote:


> Yes, this is the case. By default all variables at the top of the module
> (don't know the technical term) are public. Sticking in a private statement
> changes that for all such vars, unless they are explicitly declared public,
>
> Hope this helps,


It helps, but I was hoping I could save some typing one way or the other :)

Cheers
Neilen


--
you know its kind of tragic
we live in the new world
but we've lost the magic
-- Battery 9 (www.battery9.co.za)
Michael Metcalf

2005-04-22, 3:59 pm


"Neilen Marais" <junkmail@chatsubo.lagged.za.net> wrote in message
news:pan.2005.04.22.13.41.09.268766@chatsubo.lagged.za.net...
>
> It helps, but I was hoping I could save some typing one way or the other

:)
>

You can save some typing (and errors) by making objects public by adding
that attribute to their type specification:

module mod
private
integer, public :: a, b, c, d, e, f

The statement form with a list is really only needed for operators.

Regards,

Mike Metcalf


Richard E Maine

2005-04-22, 3:59 pm

In article <pan.2005.04.22.12.15.22.49500@chatsubo.lagged.za.net>,
Neilen Marais <junkmail@chatsubo.lagged.za.net> wrote:

> Or is the public/private thing global? IOW, if I say PRIVATE, I have
> to define each public variable as such individually?


As others have answered, yes, I'm afraid you do.

I'll note as an aside that the notion you are looking for - that
something (in this case public or private, but it generalizes) might
apply only to entities declared in a certain range of statements -
doesn't "work" very well with Fortran declarations.

One might wish it were otherwise (I do), but it is hard to pin down
exactly what statement "declares" an entity. That's because the
declarations can be scattered among multiple statements. Some entities
can be declared implicitly just by using them. There is no single
statement you can point to and definitively say that is the statement
that declares an entity. While you might choose to think of it that way,
and you might write your code with such a style (as do I), that isn't
the way that the standard defines things.

So you can't easily talk about entities that are defined in a certain
range of statements. Of course, one could tie it down more precisely,
but then its is going to start to get complicated and arbitrary seeming.

At times I wish for a form of declaration that specified "this is
everything about this entity - there can't be any attributes declared
elsewhere", but we don't have that.

--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain | experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
Walt Brainerd

2005-04-22, 4:00 pm

Richard E Maine wrote:

<snip>

> At times I wish for a form of declaration that specified "this is
> everything about this entity - there can't be any attributes declared
> elsewhere", but we don't have that.


With F, you come pretty close, since the
entity-oriented declarations are the only
ones allowed.

--
Walt Brainerd +1-877-355-6640 (voice & fax)
The Fortran Company +1-520-760-1397 (outside USA)
6025 N. Wilmot Road walt@fortran.com
Tucson, AZ 85750 USA http://www.fortran.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com