Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to initialize the std::map

Reply
Thread Tools

how to initialize the std::map

 
 
zl2k
Guest
Posts: n/a
 
      10-11-2010
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
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      10-11-2010
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.

 
Reply With Quote
 
 
 
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      10-11-2010
* 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>
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      10-11-2010
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 .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How do you initialize signals in VHDL? Madhu VHDL 7 05-28-2008 06:43 PM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
Initialize array using file i/o procedure/function? Brandon VHDL 5 09-29-2005 05:32 PM
initialize memory units Daniel Koethe VHDL 1 11-08-2004 08:36 PM



Advertisments