I used to have a normal member variable, which was initialized in the constructors as following:
ResourceSaveFunctions[OBJECTS_IDENT] = NULL;
ResourceSaveFunctions[SPRITES_IDENT] = &GMProject::SaveSprite;
ResourceSaveFunctions[SOUNDS_IDENT] = &GMProject::SaveSound;
ResourceSaveFunctions[BACKGROUNDS_IDENT] = &GMProject::SaveBackground;
ResourceSaveFunctions[PATHS_IDENT] = NULL;
ResourceSaveFunctions[SCRIPTS_IDENT] = NULL;
ResourceSaveFunctions[FONTS_IDENT] = NULL;
ResourceSaveFunctions[TIMELINES_IDENT] = NULL;
ResourceSaveFunctions[ROOMS_IDENT] = NULL;
ResourceSaveFunctions["extension"] = &GMProject::SaveExtension;
ResourceSaveFunctions[INCLUDES_IDENT] = NULL;
ResourceSaveFunctions[TRIGGERS_IDENT] = NULL;
The variable is a map with as key strings, and as data member-function-pointers. This worked perfectly fine. However as said I believe this map should be static (?) – the reason for the map is just to identify what the program should do during reading of a file. – NULL meaning “do nothing special”.
So I changed it to the following:
std::map<std::string, GMProject::GMProjectMemFn> GMProject::ResourceSaveFunctions_INIT() {
std::map<std::string, GMProjectMemFn> tmp;
tmp.insert(std::make_pair(OBJECTS_IDENT,NULL));
tmp.insert(std::make_pair(SPRITES_IDENT, &GMProject::SaveSprite));
tmp.insert(std::make_pair(SOUNDS_IDENT, &GMProject::SaveSound));
tmp.insert(std::make_pair(BACKGROUNDS_IDENT, &GMProject::SaveBackground));
tmp.insert(std::make_pair(PATHS_IDENT, NULL));
tmp.insert(std::make_pair(SCRIPTS_IDENT, NULL));
tmp.insert(std::make_pair(FONTS_IDENT, NULL));
tmp.insert(std::make_pair(TIMELINES_IDENT, NULL));
tmp.insert(std::make_pair(ROOMS_IDENT, NULL));
tmp.insert(std::make_pair("extension", &GMProject::SaveExtension));
tmp.insert(std::make_pair(INCLUDES_IDENT, NULL));
tmp.insert(std::make_pair(TRIGGERS_IDENT, NULL));
return tmp;
}
const std::map<std::string, GMProject::GMProjectMemFn> GMProject::ResourceSaveFunctions(GMProject::ResourceSaveFunctions_INIT());
Where those things are declared in the header:
static const std::map<std::string, GMProjectMemFn> ResourceSaveFunctions;
static std::map<std::string, GMProjectMemFn> ResourceSaveFunctions_INIT();
Now compiling suddenly brings up a lot of errors.
1>c:\program files\microsoft visual studio 10.0\vc\include\utility(163): error C2440: ‘initializing’ : cannot convert from ‘int’ to ‘GMProject::GMProjectMemFn ‘
Which is about the conversion of NULL. However shouldn’t this be just possible? Why is this not possible (yet in the previous method it was)?
Should I use an explicit cast here?
EDIT:
GMProjectMemFn defined as following:
typedef void (GMProject::*GMProjectMemFn)(const pTree&) const;
pTree being a container.
std::make_paircreates apair<T1, T2>where the typesT1andT2are deduced implicitly from the types of the arguments.NULLexpands to0(or0L) so in your casemake_pairreturns apair<string, int>(or apair<string, long>).You then try passing that
pair<string, int>tomap<string, GMProject::GMProjectMemFn>::insert()but this expects apair<string, GMProjectMemFn>.std::pairhas a general copy constructor which will attempt implicit conversion of each member of the pair:but in your case this requires converting a
const int&to a pointer, which is not permitted.In your original case you were directly converting
NULLinto a pointer, which is well defined.Explicitly typing your
pairshould fix this: