tags:

views:

144

answers:

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

A: 

You can omit the namespace qualifier to make it look cleaner.

using namespace std;

void myPreciousFunction(string s1 = "", string s2 = "")
{
}
Arrix
namespaces look prettier to me.
John.M
Populating the global namespace by this is fine for source files, since no other files are affected by this. _But_: Doing this in a header file will populate the global for each source and header file that includes it. It will eventually cause problems.
Maik Beckmann
A: 

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

mdma
+3  A: 

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.

shoosh
I'll go with std::string s1 = std::string() because it look great!
John.M
UncleBens
@John: You have an interesting measure of prettiness.
KennyTM
A: 
#define PRETTY std::string s1="", std::string s2=""
myPreciousFunction(PRETTY){}

:)

Jimmeh
3 down votes? Sense of humour much?
Jimmeh
A: 

from the comments....

use

using namespace std;

above all... you don't need to write std::string anywhere you can write just string....

mihirpmehta
`using` statements are best avoided in header files due to their global influence.
shoosh
yes but here was the concern of prettier look....so
mihirpmehta
@mihirpmehta It should also look prettier from inside too.
John.M
A: 

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.

Potatoswatter