myPreciousFunction(std::string s1 = "", std::string s2 = "")
{
}
int main()
{
myPreciousFunction();
}
can i make the arguments look any more prettier? i want there to be empty string if no arguments were supplied.
myPreciousFunction(std::string s1 = "", std::string s2 = "")
{
}
int main()
{
myPreciousFunction();
}
can i make the arguments look any more prettier? i want there to be empty string if no arguments were supplied.
You can omit the namespace qualifier to make it look cleaner.
using namespace std;
void myPreciousFunction(string s1 = "", string s2 = "")
{
}
An alternative is to use overloaded functions, e.g.
myPreciousFunction(std::string s1, std::string s2)
{
// primary implementation
}
myPreciousFunction(std:string s1)
{
myPreciousFunction(s1, "");
}
myPreciousFunction()
{
myPreciousFunction("", "");
}
Though I'm not sure this is any prettier, and definitely less attractive code-wise. (Default arguments are there to avoid this.)
you may consider this:
myPreciousFunction(std::string s1 = std::string(), std::string s2 = std::string())
{
}
But it doesn't really look prettier.
Also, if you're passing strings, you might want to pass them as const&:
myPreciousFunction(const std::string& s1, const std::string& s2)
{
}
This is a standard way to avoid coping the data around.
#define PRETTY std::string s1="", std::string s2=""
myPreciousFunction(PRETTY){}
:)
from the comments....
use
using namespace std;
above all... you don't need to write
std::string anywhere you can write just string....
There is actually another solution.
const std::string empty = std::string();
myPreciousFunction( const std::string &s1 = empty, const std::string &s2 = empty)
This has the advantage of avoiding construction of temporary objects.