From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,c83a22003c320b45 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1994-11-10 03:16:04 PST Path: nntp.gmd.de!newsserver.jvnc.net!nntpserver.pppl.gov!princeton!rutgers!cis.ohio-state.edu!math.ohio-state.edu!howland.reston.ans.net!gatech!news-feed-1.peachnet.edu!news.duke.edu!news.mathworks.com!panix!cmcl2!thecourier.cims.nyu.edu!thecourier.cims.nyu.edu!nobody From: comar@cs.nyu.edu (Cyrille Comar) Newsgroups: comp.lang.ada Subject: Re: Initialization Params for Controlled Types Date: 5 Nov 1994 10:01:09 -0500 Organization: Courant Institute of Mathematical Sciences Distribution: world Message-ID: <39g6nl$86v@lang8.cs.nyu.edu> References: <39ebsa$129i@watnews1.watson.ibm.com> NNTP-Posting-Host: lang8.cs.nyu.edu Date: 1994-11-05T10:01:09-05:00 List-Id: 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