Re: Ways of constructing objects.
From: Alf P. Steinbach (alfps_at_start.no)
Date: 07/04/04
- Next message: Jack Klein: "Re: weird behavior of algbraic evaluation in concatenated operators"
- Previous message: Robbie Hatley: "Ways of constructing objects."
- In reply to: Robbie Hatley: "Ways of constructing objects."
- Next in thread: Jonathan Turkanis: "Re: Ways of constructing objects."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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?
- Next message: Jack Klein: "Re: weird behavior of algbraic evaluation in concatenated operators"
- Previous message: Robbie Hatley: "Ways of constructing objects."
- In reply to: Robbie Hatley: "Ways of constructing objects."
- Next in thread: Jonathan Turkanis: "Re: Ways of constructing objects."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|