Archived

This topic is now archived and is closed to further replies.

Optional Arguments

This topic is 5243 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Whats an efficient way to have a function take optional arguments? I started out just overloading the function: one function containing both possible things to pass, another if they just pass one (always required) Now i want to add a 2nd optional argument (3rd overall argument), but am unsure the best way to do this. Basically my function is something like: int strHndlMessage(string strText, bool lnfeed, bool iscenter){ }; All it''s going to do is take the text i pass to strText and output it using cout. lnfeed is a bool for if the function is going to put a line feed at the end of the line or not. I want this to be optional, so that if i don''t specifically pass something to it, it will default to false. iscenter is also a bool that i pass if i want the text to be centered so i can do some math and center the text. This also i want to be optional, defaulting to false if nothing is passed. Anyone point me in the right direction?

Share this post


Link to post
Share on other sites
For your forward declaration, put:

int strHndlMessage(string strText, bool lnfeed = false, bool iscenter = false);

That will do exactly what you''re asking without writing multiple functions.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________

Share this post


Link to post
Share on other sites
*smacks head*

Yes... exactly. Thank you.

And i have to make sure i order them in the order of most used. I.E. I can''t have argument 2 blank and fill argument 3. doesn''t work like that.

Thanks a million.

Share this post


Link to post
Share on other sites
quote:
Original post by Mortanis
And i have to make sure i order them in the order of most used. I.E. I can''t have argument 2 blank and fill argument 3. doesn''t work like that.



Correct

Share this post


Link to post
Share on other sites