1.
int Add (int a, int b = 3);
int Add (int a, int b)
{
}
2.
int Add (int a, int b);
int Add (int a, int b = 3)
{
}
Both work; which is the standard way and why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you put the declaration in a header file, and the definition in a separate
.cppfile, and#includethe header from a different.cppfile, you will be able to see the difference.Specifically, suppose:
lib.h
lib.cpp
test.cpp
The compilation of
test.cppwill not see the default parameter declaration, and will fail with an error.For this reason, the default parameter definition is usually specified in the function declaration:
lib.h