Friday, February 8, 2013

Default Param in VC++




Declaration :
void AddVal(int iFirstVal, int iSecondVal=0);

Definition
void AddVal(int iFirstVal, int iSecondVal)
{

int iVal = iFirst+iSecond;
    cout << "Sum : " << iVal << endl;

}
 
int main()
{
    AddVal(1); // nValue2 will use default parameter of 10
    AddVal(3, 4); // override default value for nValue2
}

1. Default Parameter should be last parameter.[Otherwise hot compiler will know which attributes should be omit.]
2. No default value allowed in definition. Default value should be given only in declaration.
3. Default arguments can be provided for pointers to functions. For example:
int (*pShowIntVal)( int i = 0 );

No comments:

Followers

Link