This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
Re: Constructors for dynamically allocated CLASS data
- From: Damian Rouson <damian at rouson dot net>
- To: "fortran at gcc dot gnu dot org" <fortran at gcc dot gnu dot org>
- Date: Tue, 16 Jul 2013 07:37:39 -0700
- Subject: Re: Constructors for dynamically allocated CLASS data
> On 7/15/2013 1:29 AM, Tobias Burnus wrote:
> John McFarland wrote:
> Quick question about constructors in Fortran 2003/08. Say I have an abstract base class such as Shape, with > child classes such as Circle, Square, etc. In C++, you can dynamically allocate these data using something > like:
> Shape *s;
> s = new Circle(3); // Construct a circle with radius=3
> Well, in principle you could do likewise in Fortran 2008:
> class(shape), allocatable :: s
> s = circle(3)
In case it's of interest, my workaround for doing this in Fortran 2003 is
allocate(s,source=circle(3))
It like slightly clunky syntax but on slightly so.
Damian