Optional Variable and Deffinitions

I'm trying to make my own class and I had a few questions about functions. How do you give them an options parameter?

Example of what I think I'm trying to do:

1
2
3
4
5
6
7
void FUNCTION (string, [optional] int)
{
if (int == [true])
cout<<string;
else
cout<<"You did not give an int.";
}


Anyone know what I'm talking about and how I accomplish this?
Last edited on
Either use default parameters (if you want a default value) or overload the function.
Okay.... Can you give me an example? >_>

Oh, and I forgot to ask, how do I give definitions for functions. Sometimes when I use an external library (SFML for example), some functions have a short definition to go on.
Either by overloading:
1
2
3
4
5
6
7
8
9
void function(string)
{
  cout << "You did not give an int.";
}

void function(string str,int)
{
  cout << str;
}


or a default parameter (only if there's an int value that can be considered "invalid"):
1
2
3
4
5
void function(string str,int iParam=-1)
{
  if (i!=-1)cout << str;
  else cout << "You did not give an int.";
}


If there is no such invalid value, another option would be to use a pointer:
1
2
3
4
5
void function (string str,int* iParam)
{
  if (iParam)cout << str;
  else cout << "You did not give an int.";
}


Oh, and I forgot to ask, how do I give definitions for functions. Sometimes when I use an external library (SFML for example), some functions have a short definition to go on.

What exactly do you mean?
Last edited on
here's a screeny
http://i1109.photobucket.com/albums/h434/My_SW_pics/example.png

and thanks for the examples and references^_^
Last edited on
Topic archived. No new replies allowed.