I am trying to initialize the class FileList
, which extends std::map
as a collection of FileInfo
objects with a map specific initializer list but I receive the following error:
Error C2440 'initializing': cannot convert from 'initializer list' to 'FileList'
#include <string>
#include <map>
class FileInfo
{
public:
FileInfo(const std::string &name) : mName(name) { }
std::string operator () () const
{
return mName;
}
private:
std::string mName;
};
class FileList : public std::map<std::string, FileInfo> { };
int main(int argc, char *argv[])
{
FileInfo f1 = { "f1" };
FileInfo f2 = { "f2" };
FileList fileList{ { f1(), f1 }, { f2(), f2 } };
return 0;
}
I didn't find out why this fails, given the fact that FileList
it's exactly a std::map
.
Is there a possibility to pass the initializer list to the std::map
inherited by FileList
without inserting manually each object like below?
FileList fileList;
fileList[f1()] = f1;
fileList[f2()] = f2;
EDIT:
As @DeiDei says, it is not recommended to derive from an std::container.