Path: utzoo!attcan!uunet!microsoft!jimad From: jimad@microsoft.UUCP (Jim ADCOCK) Newsgroups: comp.lang.c++ Subject: Re: Declare at first use? (was Re: C++ coding standards) Keywords: standard,variables Message-ID: <56747@microsoft.UUCP> Date: 20 Aug 90 18:57:51 GMT References: <2161@runxtsa.runx.oz.au> <1990Aug13.182226.24141@alias.uucp> <56642@microsoft.UUCP> <1990Aug17.191939.3990@ingres.Ingres.COM> Reply-To: jimad@microsoft.UUCP (Jim ADCOCK) Organization: Microsoft Corp., Redmond WA Lines: 22 In article <1990Aug17.191939.3990@ingres.Ingres.COM> jas@llama.Ingres.COM (Jim Shankland) writes: |In article <56642@microsoft.UUCP> jimad@microsoft.UUCP (Jim ADCOCK) writes: |>Hm, I'm not sure I understand what you're trying to say here. In the |>following code: |> |> for (int i=0; i<1000; ++i) |> { |> FOO foo(i); |> foo.DoSomething(); |> } | |>It is certainly not the case that a thousand foos reside simultaneously |>on the stack. | |Yes, but if you don't want foo's constructor and destructor to be called |on each loop iteration, you probably want to declare foo outside the loop, |even if it only gets used inside the body of the loop. Yes, I agree. My understanding is that foo can be used for the side-effects of its constructor and destructor, so that the compiler does not even have the option of moving foo outside the loop. If you don't want 1000s of construction/destructions, move foo outside the loop.