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. Advertisements

  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. Advertisements

  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. Advertisements

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. Jonathan Mcdougall

    Clarification for std::vector and std::map

    Jonathan Mcdougall, Jul 30, 2003, in forum: C++
    Replies:
    5
    Views:
    625
    Sir_Ciph3r
    Jul 31, 2003
  2. Matthias Hildebrand
    Replies:
    5
    Views:
    9,178
    krogers
    Mar 20, 2012
  3. Erik Arner
    Replies:
    0
    Views:
    1,450
    Erik Arner
    Nov 2, 2004
  4. Peter Jansson
    Replies:
    5
    Views:
    6,985
    Ivan Vecerina
    Mar 17, 2005
  5. Replies:
    10
    Views:
    965
    Jerry Coffin
    Jan 18, 2008
  6. Replies:
    1
    Views:
    599
    red floyd
    Dec 21, 2008
  7. Thomas J. Gritzan
    Replies:
    6
    Views:
    1,226
    James Kanze
    Dec 22, 2008
  8. James Kanze
    Replies:
    0
    Views:
    2,251
    James Kanze
    Dec 21, 2008
Loading...