Sign in to follow this  
Enerjak

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

Recommended Posts

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......

Share this post


Link to post
Share on other sites

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).

Edited by nonoptimalrobot

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this