1.
A function parameter with an assignment in the function declaration:
It's a default value that is assigned to the parameter variable if no corresponding argument is sent to the function. Here is an example:
Code:
#include <iostream>
using namespace std;
void myfunc(int n, double d = 15.5)
{
cout<<n<<endl
<<d<<endl;
}
int main()
{
myfunc(10);
myfunc(10, 30);
return 0;
}
2. I'm building a C++ Berkeley socket wrapper and to access the close(socketDescriptor) function I need "::" in the front. What does "::" mean and do?
"::" is the scope resolution operator, and when there is no name in front of it, it resolves to the global scope. Here is an example:
Code:
#include <iostream>
using namespace std;
const int number = 10;
void myfunc(int number)
{
cout<<number<<endl
<<::number<<endl;
}
int main()
{
myfunc(1);
return 0;
}
3.
Code:
someClass::someFunction(string &message, int var2) throw() : someString(message) {
someString.append("??");
}
That code can be rearranged like this:
Code:
someClass::someFunction(string &message, int var2) throw() : someString(message)
{
someString.append("??");
}
throw() means the function is not allowed to throw any exceptions. By default, a function can throw any type of exception. throw() lets you specify which types of exceptions the function is allowed to throw between the parentheses. If you don't list any types between the parentheses, then the function can't throw any exceptions. Note: VC++6 does not support throw().
The fragment:
: someString(message)
appears to be an initializer list, which is used in a constructor to initialize the member variables. I say "appears" because an intializer list like that is used with a constructor, so that would mean the actual code should look like this:
Code:
someClass::someClass(string &message, int var2) throw() : someString(message)
Note the use of the scope resolution operator here to indicate the function you are defining is the one by that name in someClass.