how to initialize the std::map

Discussion in 'C++' started by zl2k, Oct 11, 2010.

  1. zl2k

    zl2k Guest

    hi, there,

    I have a class

    class Foo{
    public:
    std::map<int, int> bar;
    Foo();
    }


    Foo::Foo(): bar(){// am I doing right? I need to initialize an empty
    bar
    }

    Thanks for help.

    zl2k
     
    zl2k, Oct 11, 2010
    #1
    1. Advertising

  2. zl2k

    red floyd Guest

    On 10/10/2010 7:11 PM, zl2k wrote:
    > hi, there,
    >
    > I have a class
    >
    > class Foo{
    > public:
    > std::map<int, int> bar;
    > Foo();
    > }
    >
    >
    > Foo::Foo(): bar(){// am I doing right? I need to initialize an empty
    > bar
    > }
    >


    Yes, that is correct.

    However, you don't even need to do that.

    Foo::Foo() { }

    Will call bar's constructor in the exact same manner.
     
    red floyd, Oct 11, 2010
    #2
    1. Advertising

  3. * zl2k, on 11.10.2010 04:11:
    > hi, there,
    >
    > I have a class
    >
    > class Foo{
    > public:
    > std::map<int, int> bar;
    > Foo();
    > }
    >
    >
    > Foo::Foo(): bar(){// am I doing right? I need to initialize an empty bar
    > }


    It's OK[1] but unnecessary -- std::map provides default initialization.


    Cheers & hth.,

    - Alf

    Notes:
    [1] Modulo the missing semicolon.

    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Oct 11, 2010
    #3
  4. zl2k

    Jorgen Grahn Guest

    On Mon, 2010-10-11, Alf P. Steinbach /Usenet wrote:
    > * zl2k, on 11.10.2010 04:11:
    >> hi, there,
    >>
    >> I have a class
    >>
    >> class Foo{
    >> public:
    >> std::map<int, int> bar;
    >> Foo();
    >> }
    >>
    >>
    >> Foo::Foo(): bar(){// am I doing right? I need to initialize an empty bar
    >> }

    >
    > It's OK[1] but unnecessary -- std::map provides default initialization.


    It helps me to think of it this way: C++ makes it really, really hard
    to accidentally create an uninitialized object[1]. With just the class
    defined as above it compiles, and the only rational explanation is
    that the std::map default constructor actually gets run!

    /Jorgen
    [1] Modulo malloc, memcpy and so on. You can come up with more examples,
    I'm sure.

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Oct 11, 2010
    #4
    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. Peter Jansson
    Replies:
    5
    Views:
    6,423
    Ivan Vecerina
    Mar 17, 2005
  2. Replies:
    10
    Views:
    804
    Jerry Coffin
    Jan 18, 2008
  3. Replies:
    1
    Views:
    451
    red floyd
    Dec 21, 2008
  4. Thomas J. Gritzan
    Replies:
    6
    Views:
    1,047
    James Kanze
    Dec 22, 2008
  5. James Kanze
    Replies:
    0
    Views:
    2,065
    James Kanze
    Dec 21, 2008
Loading...

Share This Page