Topic: POD Member Initialization


Author: wmm@fastdial.net
Date: 2000/07/29
Raw View
In article <z%fg5.10493$sO2.43414@typhoon.tampabay.rr.com>,
  "Scott Robert Ladd" <scottrobertladd@hotmail.com> wrote:
> When initializing a POD member in a constructor, is it better to use
the
> "constructor" syntax or to make an assignment in the constructor
body?

When I teach C++, I require the students to use initialization
instead of assignment.  For POD types, there's no difference
in the effects, of course, but I think there are at least a
few good arguments in favor of always using initialization:

1) Consistency.  If you always use the initialization list,
you'll never inadvertently use assignment instead of
initialization for a non-POD type, where there might be a
difference in performance or in the semantics.  Consistency
is also helpful for readers of the code: a reader always
knows where to look for the initial value of a member, and
if you forget to initialize a member, it's easier to spot
the omission than if "initializations" are split between the
mem-init-list and the body of the constructor.

2) Resistance to change.  You might replace a POD-type
member with a non-POD member and forget to change the
constructor from assignment to initialization, which could
have adverse consequences (I'm thinking of changing char*
to String or float to MyExtendedFixedPoint, where the
initialization would likely not change).  You might also
decide that a member didn't need to be modifiable and
change it to be const; the compiler will remind you to
change the assignment to initialization if you forget, but
if it were already initialized instead of assigned no edit
would be required.

3) Reduced size of constructor bodies.  If anything
_interesting_ occurs in a constructor beyond setting the
initial values of members, it stands out more and is easier
to see if it's not embedded in a batch of straightforward
assignments.  (In fact, it's a good flag to a reader if the
body of a constructor is anything _other_ than "{ }".)

--
William M. Miller, wmm@fastdial.net
Vignette Corporation (www.vignette.com)


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: comeau@panix.com (Greg Comeau)
Date: 2000/07/29
Raw View
In article <z%fg5.10493$sO2.43414@typhoon.tampabay.rr.com>,
Scott Robert Ladd <scottrobertladd@hotmail.com> wrote:
>When initializing a POD member in a constructor, is it better to use the
>"constructor" syntax or to make an assignment in the constructor body? In
>other words, is:
>
>    class Foo1
>    {
>    public:
>        int m_value;
>
>        Foo1() : m_value(0) { }
>    };
>
>better than (or in practical terms, different from):
>
>    class Foo2
>    {
>    public:
>        int m_value;
>
>        Foo2() { m_value = 0; }
>    };
>
>I generally follow the second (Foo2) pattern, since I started coding back in
>the days before C++ compilers supported "constructors" for POD types.
>Someone asked me, however, why I do Foo2 over Foo1, and I really didn't have
>an answer based on any practical considerations. So is there any practical
>difference between the two styles?

Check out http://www.comeaucomputing.com/techtalk/#meminit

- Greg
--
Comeau Computing / Comeau C/C++ 4.2.42 (4.2.44 expected soon)
TRY Comeau C++ ONLINE at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Scott Robert Ladd" <scottrobertladd@hotmail.com>
Date: 2000/07/29
Raw View
When initializing a POD member in a constructor, is it better to use the
"constructor" syntax or to make an assignment in the constructor body? In
other words, is:

    class Foo1
    {
    public:
        int m_value;

        Foo1() : m_value(0) { }
    };

better than (or in practical terms, different from):

    class Foo2
    {
    public:
        int m_value;

        Foo2() { m_value = 0; }
    };

I generally follow the second (Foo2) pattern, since I started coding back in
the days before C++ compilers supported "constructors" for POD types.
Someone asked me, however, why I do Foo2 over Foo1, and I really didn't have
an answer based on any practical considerations. So is there any practical
difference between the two styles?

** Scott Robert Ladd
 * http://www.coyotegulch.com


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: sharris@nospam.primus.com (Steven E. Harris)
Date: 2000/07/29
Raw View
"Scott Robert Ladd" <scottrobertladd@hotmail.com> writes:

[...]

> Someone asked me, however, why I do Foo2 over Foo1, and I really didn't have
> an answer based on any practical considerations. So is there any practical
> difference between the two styles?

Scott Meyers' Effective C++, Item 12 - "Prefer initialization to
assignment in constructors" - would give you the argument you need in
support of the first style. For POD types, the difference isn't as
important, but consistency may be a sufficient motivator to make you
favor the first style.

--
Steven E. Harris
Primus Knowledge Solutions, Inc.
http://www.primus.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: David R Tribble <david@tribble.com>
Date: 2000/08/03
Raw View
"Scott Robert Ladd" <scottrobertladd@hotmail.com> wrote:
>> When initializing a POD member in a constructor, is it better to use
>> the "constructor" syntax or to make an assignment in the constructor
>> body?

wmm@fastdial.net wrote:
> When I teach C++, I require the students to use initialization
> instead of assignment.  For POD types, there's no difference
> in the effects, of course, but I think there are at least a
> few good arguments in favor of always using initialization:
>
> 1) Consistency.  ...
>
> 2) Resistance to change.  You might replace a POD-type
> member with a non-POD member and forget to change the
> constructor from assignment to initialization, which could
> have adverse consequences ...
>
> 3) Reduced size of constructor bodies.  ...

I personally use the member-initializer (constructor) syntax for
reason (1), consistency.

Another reason is that const and reference members can only be
initialized in this way.

Yet another reason is that the member-initialization syntax can
save a redundant assignment.

The only drawback is that array members cannot be initialized this
way; they must be initialized with a loop in the constructor
function body.  This is a misfeature of C++.

--
David R. Tribble, mailto:david@tribble.com, http://david.tribble.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]