M
Miles Bader
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
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