Re: What's wrong with this code?
On May 9, 11:11 am, loose AT astron DOT nl <l...@astron.nl> wrote:
Hi,
Could anyone tell me what's wrong with the code below? It segfaults
when the first element is being inserted in the map.
There is no map - yet.
<code>
#include <map>
#include <string>
#include <iostream>
using namespace std;
class Init
{
public:
Init();
private:
static map<int, string> map_;
static Init init_;
};
Init::Init()
{
map_[1] = "One"; // <--- Segfault here
}
Init Init::init_;
The above invokes Init's ctor before the static map comes to life.
try switching these two statements.
After all, isn't it natural that the members of a type be constructed
first?
map<int, string> Init::map_;
int main()
{
return 0;}
</code>
Kind regards,
Marcel Loose.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]