comp.lang.ada
 help / color / mirror / Atom feed
From: comar@cs.nyu.edu (Cyrille Comar)
Subject: Re: Initialization Params for Controlled Types
Date: 5 Nov 1994 10:01:09 -0500
Date: 1994-11-05T10:01:09-05:00	[thread overview]
Message-ID: <39g6nl$86v@lang8.cs.nyu.edu> (raw)
In-Reply-To: 39ebsa$129i@watnews1.watson.ibm.com

ncohen@watson.ibm.com (Norman H. Cohen) writes:
: In article <1994Nov4.134412.10010@unix.brighton.ac.uk>,
: je@unix.brighton.ac.uk (John English) writes: 
: 
: |> There is one subtle danger with using procedures as "constructors".
: |> Imagine the following: 
: |>   type BankAccount is tagged private;
: |>   procedure Open (Account : in out BankAccount;
: |>                Name    : in String);
: |>
: |> Here Open opens a bank account in a particular name.  Now if you
: |> derive a new type CurrentAccount from BankAccount to produce a
: |> BankAccount that allows overdrafts, you might have this: 
: |>   type CurrentAccount is new BankAccount with private;
: |>   procedure Open (Account   : in out CurrentAccount;
: |>                Name      : in String;
: |>                Overdraft : in Money);
: |>
: |> Here's where the danger is: the first procedure (Open for BankAccount)
: |> is a primitive of BankAccount and will be inherited by CurrentAccount.
: |> It's therefore possible to initialise a CurrentAccount as if it were
: |> a BankAccount, which might leave the overdraft limit uninitialised: 
: |>   C : CurrentAccount;
: |>   ...
: |>   Open (C, "Fred Bloggs");  -- oops!
: |>
: |> Okay, you can provide a default overdraft limit of 0.00, so this is
: |> ...
: |> C++ sidesteps this problem by making constructors non-inheritable.
: |> The only way to accomplish this in 9X is to make sure that Open isn't
: |> a primitive (e.g. by shoving it in a child package) which is fairly
: |> painful.

you have an exact equivalent in Ada. If you don't want to make Open a primitive
operation of BankAccount define it as a classwide operation:

  procedure Open (Account : in out BankAccount'Class;
                  Name    : in String);

It will not be inherited! I do not say that it is the perfect solution for your
problem, my point is that it is exactly equivalent to defining Open as a
non-virtual member function in C++.

-- 
------------------------------------------------------------------------
Cyrille Comar,                                  E-mail: comar@cs.nyu.edu
Gnat Project                                    US phone: (212) 998-3489




  reply	other threads:[~1994-11-05 15:01 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-10-27  9:44 Initialization Params for Controlled Types Angel Alvarez
1994-10-27 14:27 ` Tucker Taft
1994-11-04 13:44   ` John English
1994-11-04 22:16     ` Norman H. Cohen
1994-11-05 15:01       ` Cyrille Comar [this message]
1994-11-07 10:32         ` John English
1994-11-07  9:08       ` John English
1994-11-10 15:23       ` John English
1994-11-11 10:44         ` Robb Nebbe
1994-11-14 21:19         ` Norman H. Cohen
1994-11-14 18:35           ` Robert I. Eachus
1994-11-16 21:45           ` Matt Kennel
1994-10-27 23:06 ` Robert Dewar
replies disabled

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