a map<T,T> as a default parameter...

Started by
2 comments, last by NightCreature83 10 years, 7 months ago

I'm trying to add something like this in my engine (since I HAVE TO REWRITE the WHOLE things AGAIN!!!! sorry, Just mad). Anyways, I thought I'd go with something like this:


bool createWindowImp(DemString winName,	// window name
			DemString winTitle,	// window title
			DemUInt winWidth,		// window width.
			DemUInt winHeight,
			ExtraParameters params);

the ExtraParameters params is a typde def of a map<string, string> Now I tried to put a default value of 0 for params so it wouldn't need to be put unless otherwise used, but C++ didn't like that very much.... here's the error:


1>c:\users\rosario\documents\visual studio 2008\projects\dementedengine\dementedcore\demwindow.h(36) : error C2440: 'default argument' : cannot convert from 'int' to 'Demented::ExtraParameters'
1>        No constructor could take the source type, or constructor overload resolution was ambiguous

I'm sure the answer is as easy as pi.....just wish I knew what to do......

Advertisement

You would need to do this:


bool createWindowImp(DemString		winName,	// window name
		     DemString		winTitle,	// window title
		     DemUInt		winWidth,	// window width.
		     DemUInt		winHeight,
		     ExtraParameters	params = ExtraParameters());

Which will push an instance of ExtraParameters (which will be empty) onto the stack and pass it on to the function. The instance will get pushed off the stack after the function's scope terminates. A more efficient approach would look like this:


bool createWindowImp(DemString		winName,	// window name
		     DemString		winTitle,	// window title
		     DemUInt		winWidth,	// window width.
		     DemUInt		winHeight,
		     ExtraParameters*	params = 0);

Inside createWindowImp you will need to check to see if param is non-null and proceed to dereference it and extract values if so.

[EDIT]

I see ExtraParameters is an std::map, in that case IF you want to go with the fist option you will definitely want to tweak it a bit:


bool createWindowImp(DemString			winName,	// window name
		     DemString			winTitle,	// window title
		     DemUInt			winWidth,	// window width.
		     DemUInt			winHeight,
		     const ExtraParameters&	params = ExtraParameters());

If you don't pass by reference then each time the function is called a temporary std::map will be created for the params varaible and a deep copy will be preformed between it and whatever you happen to be passing in; that equates to a lot of memory that gets newed only to be prompty deleted. It's good practice to toss the 'const' in there too unless you want to pass info out of createWindowImp via params (usually forwned upon).

thanks, that worked.

Just as a side note but in general strings should be passed as const reference arguments and that is very much true for the map<T,T> as well. All big objects that don't need changing in the function you are calling should be passed by const reference

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

This topic is closed to new replies.

Advertisement