Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Enerjak   Members   -  Reputation: 231

Like
0Likes
Like

Posted 27 August 2013 - 11:21 PM

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



Sponsor:

#2 nonoptimalrobot   Members   -  Reputation: 408

Like
2Likes
Like

Posted 28 August 2013 - 12:18 AM

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, 28 August 2013 - 12:28 AM.


#3 Enerjak   Members   -  Reputation: 231

Like
0Likes
Like

Posted 28 August 2013 - 12:45 AM

thanks, that worked.



#4 NightCreature83   Crossbones+   -  Reputation: 2703

Like
1Likes
Like

Posted 28 August 2013 - 03:32 AM

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, Mad Max




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS