Path: utzoo!attcan!uunet!microsoft!jimad From: jimad@microsoft.UUCP (Jim ADCOCK) Newsgroups: comp.lang.c++ Subject: Re: conditional scope and destructors Message-ID: <59016@microsoft.UUCP> Date: 12 Nov 90 19:19:55 GMT References: <1990Nov9.151803.11426@hemel.bull.co.uk> Reply-To: jimad@microsoft.UUCP (Jim ADCOCK) Organization: Microsoft Corp., Redmond WA Lines: 53 In article <1990Nov9.151803.11426@hemel.bull.co.uk> pmoore@hemel.bull.co.uk (Paul Moore) writes: >This is a problems I had writing dos code with zortech 2.1 but it is a general >problem with c++ - something is not tightly defined. > >Problem:- if an object is only created conditionally in a function should >its destructor be called at function exit? > >Example:- > >func(..) >..... > if (foo==bar) > return(z); >..... > object obj1; >.... >} >If foo does equal bar then obj is not created - should its destructor be >called when the return is done? Zortech 2.1 does call it. Issues of construction and destruction in the presence of conditional statements has been a confusing issue in the past -- especially since most compilers seem to have some problems in these areas. Section 6 seems to clarify what is expected considerably: A declaration is a statement that is expected to be executed everytime it is hit. For every time an object is constructed, it is expected to be destructed. A jump forward over an initializer statement is illegal unless that initializer statement is in an inner block wholey skipped over. A jump backwards over an initializer statement is legal and causes the destructor to be invoked before the object is initialized again. An object's destructor is not invoked unless that object has been constructed, and exiting a scope destroys objects constructed therein. Translated into pidgin C, the results of your example should be as if: func(..) { ..... if (foo==bar) { return(z); } else { ..... struct object obj1; construct_object(&obj1); .... destruct_object(&obj1); } }