Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

optional parameters

Status
Not open for further replies.

mangocinn

Programmer
Jul 26, 2001
66
US
In C++, how do I declare a function that has an optional parameter?

I searched the knowledge base and was not able to find anything about optional parameters.

Does setting the default value of the parameter in the function declaration automatically make it optional?


void MyFunction(const std::string& parameter1, const std::string& parameter2);
I need to make parameter2 optional and default it to parameter1.
 
Yes, setting a default value automatically makes a parameter optional. However, you cannot default an optional parameter to the value of another parameter. One way to get around this is to overload the function like this:
Code:
void MyFunction(const std::string& parameter1)
{
  MyFunction(parameter1, parameter1);
}
void MyFunction(const std::string& parameter1, const std::string& parameter2);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top