Ways of constructing objects.

Discussion in 'C++' started by Robbie Hatley, Jul 4, 2004.

  1. A few days ago, tom_usenet <> 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.

    --
    Cheers,
    Robbie Hatley
    Tustin, CA, USA
    email: lonewolfintj at pacbell dot net
    web: home dot pacbell dot net slant earnur slant
     
    Robbie Hatley, Jul 4, 2004
    #1
    1. Advertising

  2. * "Robbie Hatley" <lonewolfintj at pacbell dot net>:
    > A few days ago, tom_usenet <> 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?
     
    Alf P. Steinbach, Jul 4, 2004
    #2
    1. Advertising

  3. "Robbie Hatley" <lonewolfintj at pacbell dot net> wrote in message
    news:40e78441$0$5859$...
    > A few days ago, tom_usenet <> 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();

    >


    You can find a useful discussion and some references here:
    http://www.boost.org/libs/utility/value_init.htm.

    Jonathan
     
    Jonathan Turkanis, Jul 4, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leo Muller
    Replies:
    2
    Views:
    438
    Paul Glavich [MVP - ASP.NET]
    Jun 16, 2004
  2. Archimede

    concurrency constructing objects

    Archimede, Nov 25, 2005, in forum: Java
    Replies:
    5
    Views:
    418
    Chris Uppal
    Dec 1, 2005
  3. Replies:
    1
    Views:
    327
    Alf P. Steinbach
    Feb 23, 2006
  4. Replies:
    5
    Views:
    444
    =?iso-8859-1?q?Kirit_S=E6lensminde?=
    Mar 6, 2007
  5. 7stud
    Replies:
    11
    Views:
    700
    Dennis Lee Bieber
    Mar 20, 2007
Loading...

Share This Page