Translating normal member variables to static member variables results in problems?

Up vote 3 down vote favorite share g+ share fb share tw.

I used to have a normal member variable, which was initialized in the constructors as following: ResourceSaveFunctionsOBJECTS_IDENT = NULL; ResourceSaveFunctionsSPRITES_IDENT = &GMProject::SaveSprite; ResourceSaveFunctionsSOUNDS_IDENT = &GMProject::SaveSound; ResourceSaveFunctionsBACKGROUNDS_IDENT = &GMProject::SaveBackground; ResourceSaveFunctionsPATHS_IDENT = NULL; ResourceSaveFunctionsSCRIPTS_IDENT = NULL; ResourceSaveFunctionsFONTS_IDENT = NULL; ResourceSaveFunctionsTIMELINES_IDENT = NULL; ResourceSaveFunctionsROOMS_IDENT = NULL; ResourceSaveFunctions"extension" = &GMProject::SaveExtension; ResourceSaveFunctionsINCLUDES_IDENT = NULL; ResourceSaveFunctionsTRIGGERS_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 GMProject::ResourceSaveFunctions_INIT() { std::map 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 GMProject::ResourceSaveFunctions(GMProject::ResourceSaveFunctions_INIT()); Where those things are declared in the header: static const std::map ResourceSaveFunctions; static std::map 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. C++ static-members link|improve this question edited Nov 24 '11 at 17:25 asked Nov 24 '11 at 17:17paul23743210 67% accept rate.

I am not clear on what your big map o' member function pointers is needed for...it looks like something from a C program. – John Zwinck Nov 24 '11 at 17:25 @Kerrek_SB "partially" - using visual studio 2010 - but no I can't use initializer lists. – paul23 Nov 24 '11 at 17:26 @JohnZwinck Well this is used for something like "xml files" - during saving normally a standard approach can be used - which consists of reading the xml value, which contains a file location and then copying that file.

However as I don't have control over the format of the xml files, in some specific cases something extra should occur. For these cases I wrote a seperate function. – paul23 Nov 24 '11 at 17:30.

Std::make_pair creates a pair where the types T1 and T2 are deduced implicitly from the types of the arguments. NULL expands to 0 (or 0L) so in your case make_pair returns a pair (or a pair). You then try passing that pair to map::insert() but this expects a pair.

Std::pair has a general copy constructor which will attempt implicit conversion of each member of the pair: template pair (const pair &p) : first(p. First), second(p. Second) { } 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 NULL into a pointer, which is well defined. Explicitly typing your pair should fix this: tmp. Insert(std::pair(TIMELINES_IDENT, NULL)).

So a pair should indeed be convertible to a pair as long as int is convertible to GMProjectMemFn. – Christian Rau Nov 24 '11 at 18:44 Yes, you're completely right. The real problem is that a int const& can't be implicitly cast to a pointer.

I'll correct my answer. Thanks for pointing that out! – atkins Nov 24 '11 at 19:15.

Ah. NULL is probably defined only as literal 0, make_pair deduces this to integer, and pair is not convertible to pair. It worked with assignment through operator because (I guess) of implicit conversion from 0 to GMProjectMemFn.

So, try writing make_pair(PATHS_IDENT, (GMProjectMemFn)0).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions