Re: Ways of constructing objects.

From: Alf P. Steinbach (alfps_at_start.no)
Date: 07/04/04


Date: Sun, 04 Jul 2004 04:41:23 GMT


* "Robbie Hatley" <lonewolfintj at pacbell dot net>:
> A few days ago, tom_usenet <tom_usenet@hotmail.com> wrote
> in a message in this group, concerning ways of constructing
> objects:
>
> > > int main(void)
> > > {
> > > MyStruct Blat; // Zeros members; calls only one constructor
> > > }
> >
> > Yup, that's the best plan, but in generic code where you want default
> > initialization but don't know the type, you should probably do:
> >
> > T t = T();
>
> So, Tom (or anyone who knows something about this issue):
> You're saying I should construct objects like this:
>
> MyType Blat = MyType();
>
> Instead of:
>
> MyType Blat;
>
> What's the difference? Why would the former provide
> "default initialization" but the latter not?
>
> As for "in generic code", I tried using former form
> inside a template class and got the error message
> "ISO C++ forbids initialization of member Blat".
> But the latter form worked OK.
>
> So I'm just wondering what this "T t = T()" business
> is all about.

§8.5/9, effectively, the form "T t = T" does not initialize t when
T is a non-static POD (in the 1997 standard non-static was implied,
in the 2003 standard, Technical Corrigendum 1, it is explicit).

There is a difference between 1997 and 2003 standard regarding
"T t = T()". 1997 $8.5/7: default-initialization. 2003: "value-
initialization".

Andrew Koenig was the man, and he has explained this subtle change
very clearly a number of times in this group; unfortunately my brain
does not seem able to retain the clear understanding I always have
after reading Andrew's explanations, and the definition in the HS
involves phrasing such as "the constructor ... is called" (which
invariably makes a swarm of buzzing flies descend on me when I use it
in this newsgroup), and I'm too lazy to Google, so, I'll pass...

-- 
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Relevant Pages

  • Re: Problems I have with 1.999...=2
    ... >>Surreal numbers and infinitesimals are not outside standard logic(end ... He is constructing them from the empty set (not from the elements of the ... convince you that the surreals are not outside standard logic? ...
    (sci.math)
  • Re: 12" planers
    ... > Is 13" a new standard size? ... My guess would be that 12" is a commonly used width when constructing many ... projects and planing something of that width would require a blade a little ... Prev by Date: ...
    (rec.woodworking)
  • Ways of constructing objects.
    ... in a message in this group, concerning ways of constructing ... > initialization but don't know the type, ... As for "in generic code", ... home dot pacbell dot net slant earnur slant ...
    (comp.lang.cpp)
  • Re: Ways of constructing objects.
    ... > in a message in this group, concerning ways of constructing ... >> Yup, that's the best plan, but in generic code where you want ... Jonathan ...
    (comp.lang.cpp)