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=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109fba,f92fbb4a0420dd57 X-Google-Attributes: gid109fba,public X-Google-Thread: 103376,f92fbb4a0420dd57 X-Google-Attributes: gid103376,public From: Mike Young Subject: Re: some questions re. Ada/GNAT from a C++/GCC user Date: 1996/03/31 Message-ID: <315F4A9D.7E6F@mcs.com>#1/1 X-Deja-AN: 145163236 references: <315D902C.6F7B@escmail.orl.mmc.com> <4jmuj5$lkh@dayuc.dayton.saic.com> content-type: text/plain; charset=us-ascii organization: Fen Software, Inc. mime-version: 1.0 newsgroups: comp.lang.ada,comp.lang.c++ x-mailer: Mozilla 2.0GoldB1 (Win95; I) Date: 1996-03-31T00:00:00+00:00 List-Id: John G. Volan wrote: > > One difficulty I see with intermingling declarations and statements is > how to interpret declarations within conditional or iterative constructs. > For instance: > > -- This is NOT Ada, this is CRAPOLA (C-Reminiscent Ada-like Perversion > -- Of Language Aspects) :-) : > begin > ... > if Smaller then > X : Integer; > ... > elsif Bigger then > X : Long_Integer; > ... > end if; > ... > -- is X in scope here, and if so, what the heck is it? > ... ========== (Cute acronym; not sure if I like it though. :) The scoping rules in C++ are quite simple: the object comes into existence at the point of definition, and persists until the end of the enclosing scope. In your example, X would is created at its declaration (eg.: X : Long_Integer;), and is destroyed at the corresponding elsif or endif. Sprinkling declarations willy-nilly throughout the code is more than a matter of convenience. Construction of objects sometimes entail non-trivial construction. Rather than force a two step process -- construction at declaration, and then subsequent initialization -- it is very common to declare the object "just-in-time," and combine the two steps. This frees the object designer from allowing for an undefined state, which would exist if construction were separated from initialization. Personally, I like it this way. It's very easy to verify that all objects are correctly initialized before use when all objects are initialized at their points of construction. The object does not exist before it is initialized. Mike.