Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #5194

advice -- temp var for constructors?

From Miles Bader <miles@gnu.org>
Newsgroups comp.lang.c++
Subject advice -- temp var for constructors?
Date 2011-05-20 13:35 +0900
Organization NEC Electronics
Message-ID <buo1uzuxa3i.fsf@dhlpc061.dev.necel.com> (permalink)

Show all headers | View raw


Inside a class constructor, I often wish I could use temporary
intermediate while calculating the initialization values -- both for
clarity and efficiency -- but there doesn't seem to be any reallly good
way to do this.

E.g., consider a class A with fields x, y, z.  I want to initialize x,
y, and z from a constructor argument "arg".  If I was writing normal
code, I could do something like:

   int temp1 = some_long_calculation (arg);
   int temp2 = some_other_long_calculation (arg);
   x = blah_blah (temp1, temp2);
   y = oink_oink (temp1, temp2);
   z = zergh_zergh (temp1, temp2);

but inside constructor initialization forms, one can't create "temp1"
and "temp2".

Soooo, possible workarounds:

(1) Don't use initialization forms at all (at least for data members
    that use a temp variable), but instead just write code like the
    above in the constructor body.

    [This often works, but I think would generally be considered kind of
    ugly, and in some cases is less efficient.]

(2) Add private class data members for each temporary variable, and only
    actually use them during the initialization.

    [This is also ugly, wastes space in each object, and in some cases
    using data members as temporaries seems to result in less efficient
    code than real temporary variables (maybe because alias analysis is
    more difficult).]

(3) Just bite the bullet, and don't use temporary variables at all --
    instead duplicate the expressions and hope the compiler can detect
    the duplication and eliminate the redundancy itself.

    [Less readable, often less efficient, less maintainable because of
    the duplication.]

None of these solutions seems very satisfying.  Does anybody have advice
about other, better, methods...?

Thanks,

-Miles

-- 
Arrest, v. Formally to detain one accused of unusualness.

Back to comp.lang.c++ | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

advice -- temp var for constructors? Miles Bader <miles@gnu.org> - 2011-05-20 13:35 +0900
  Re: advice -- temp var for constructors? "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-05-20 07:16 +0200
  Re: advice -- temp var for constructors? "Balog Pal" <pasa@lib.hu> - 2011-05-20 10:38 +0200
    Re: advice -- temp var for constructors? Miles Bader <miles@gnu.org> - 2011-05-20 17:50 +0900
      Re: advice -- temp var for constructors? "Balog Pal" <pasa@lib.hu> - 2011-05-20 11:12 +0200
  Re: advice -- temp var for constructors? Marc <marc.glisse@gmail.com> - 2011-05-20 10:23 +0000
    Re: advice -- temp var for constructors? "Balog Pal" <pasa@lib.hu> - 2011-05-20 13:02 +0200
      Re: advice -- temp var for constructors? Marc <marc.glisse@gmail.com> - 2011-05-20 13:55 +0000
        Re: advice -- temp var for constructors? Marc <marc.glisse@gmail.com> - 2011-05-20 14:28 +0000

csiph-web