For Programmers: Free Programming Magazines  


Home > Archive > Fortran > May 2005 > default constructor









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 default constructor
Levent

2005-05-19, 4:01 pm

Hi,

Is there a default constructor concept for Fortran derived data types?

In particular, I have this derived type:

type Foo
integer :: size
integer, pointer(:) :: p
end type

and when a variable of this type is declared:

type(Foo) :: f

I need to have (automatically, not with a user called subroutine) it set
such that
f%size = 0
allocate(f%p(1))

thx
- L.
beliavsky@aol.com

2005-05-19, 4:01 pm

Levent wrote:
> Hi,
>
> Is there a default constructor concept for Fortran derived data

types?
>
> In particular, I have this derived type:
>
> type Foo
> integer :: size
> integer, pointer(:) :: p
> end type
>
> and when a variable of this type is declared:
>
> type(Foo) :: f
>
> I need to have (automatically, not with a user called subroutine) it

set
> such that
> f%size = 0


To get this, you can modify the declaration size in type Foo to

integer :: size = 0

> allocate(f%p(1))


I don't think this is possible.

Joost

2005-05-19, 4:01 pm

It's not possible, but you could initialise the pointer with
'=> NULL()' (which is not the same, but at least the pointer isn't
undefined anymore, so you could use 'IF (ASSOCIATED(f%p))' safely.

Joost

Richard E Maine

2005-05-19, 4:01 pm

In article <1116523381.378652.249640@o13g2000cwo.googlegroups.com>,
beliavsky@aol.com wrote:

> Levent wrote:

[wants what we call default initialization with...]

>
> To get this, you can modify the declaration size in type Foo to
>
> integer :: size = 0


Yes.

>
> I don't think this is possible.


Correct. As Joost points out that you can default initialize to
disassociated, but not to an associated state. There have been proposals
on occasion to change this, but none have made it through so far. And,
as far as I recall, all the proposals like that have involved default
initialization to associated with a named target - not to allocating an
anonymous target.

At one time, there were proposals for initial procedures, which can be
far more flexible than the simple default initialization we have now.
They could have easily handled this (and much more). Initial procedures
were similar to the final procedures that are added in f2003. But
initial procedures had a host of extra complications and ended up
getting dropped. Although desirable, they were seen as having a higher
cost and a lower payoff than final procedures. (And one of their main
proponents procrastinated serious work on them for way too long,
assuming that they would be simple. That assumption turned out to be
flawed, with the result that adding them would have meant some pretty
big changes pretty late - a recipe for serious integration problems.)
Anyway, that's how I saw it all.

--
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
Sponsored Links







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

Copyright 2009 codecomments.com