From: je@unix.brighton.ac.uk (John English)
Subject: Re: Initialization Params for Controlled Types
Date: Fri, 4 Nov 1994 13:44:12 GMT
Date: 1994-11-04T13:44:12+00:00 [thread overview]
Message-ID: <1994Nov4.134412.10010@unix.brighton.ac.uk> (raw)
In-Reply-To: CyC5I5.I93@inmet.camb.inmet.com
Tucker Taft (stt@spock.camb.inmet.com) wrote:
: For a limited object, it is also generally reasonable
: to call an explicit initialization procedure with any number
: of parameters. Again, the primary goal of Initialize is to
: provide *default* initialization. Explicit parameterized
: initialization can be performed in other ways (via functions
: or procedures).
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
not an entirely convincing example; the point is that there is a
danger here which is non-obvious, since you might forget that there
is a second Open for CurrentAccounts which is provided due to inheritance.
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.
--
-------------------------------------------------------------------------------
John English | Thoughts for the day:
Dept. of Computing | - People who live in windowed environments
University of Brighton | shouldn't cast pointers
E-mail: je@brighton.ac.uk | - In C++ only your friends can access your
Fax: 0273 642405 | private parts
-------------------------------------------------------------------------------
next prev parent reply other threads:[~1994-11-04 13:44 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 [this message]
1994-11-04 22:16 ` Norman H. Cohen
1994-11-05 15:01 ` Cyrille Comar
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