I'm trying to populate an unordered_map [class member] in a method called in the Constructor initializer list. I get a runtime exception at map[0] = 1 which seems to be due to the member not being constructed prior to the method being called. (I put a breakpoint on the unordered_map constructor and it was not hit). Is there a way to force the construction of the unordered_map member prior to the method being called?
#include <iostream>
#include <unordered_map>
class Base
{
public:
Base(int chan) { }
};
class Foo : public Base
{
std::unordered_map<int, int> map;
int Init(int chan)
{
// fill map
map[0] = 1;
map[1] = 3;
return map[chan];
}
public:
Foo(int chan) : Base(Init(chan)) { }
};
int main()
{
Foo foo(0);
}
Using Visual Studio 2019.
Off the top, here are two ideas I have. First:
This forces
mapto be constructed beforeBase.Second:
This uses delegating constructor trick to essentially simulate a local variable in the constructor initializer list.