Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

Question re: functions

If you were to have a case statement in which a function is called for each of the 8 cases, however, a file

case 1:
   runProgram(vector1, filename, filename2);

case 2:
   runProgram(vector2, filename, filename2);

case 3:
   runProgram(vector3, filename, NULL):

However, with case3, a second filename is not needed.  Is it sufficient to pass in NULL????  I would rather not create 2 separate functions since it would be rather redundant.

0
jewee
Asked:
jewee
1 Solution
 
Jaime OlivaresCommented:
Yes, you are right, don't have to make a new function, just have to evaluate if NULL inside the function, but this will work only if filename2 is a pointer (by example a pointer to a char array or a pointer to a string object)
Also you can exploit the "default value" feature of C++. By example:

yourFunc(int firstArg, char *secondArg=NULL);

you can invoke it these ways:

yourFunc(15,"a string");
yourFunc(15,NULL);
yourFunc(15);   // has the same effect of previous
0
 
pankajtiwaryCommented:
Hi jewee,
You can use the default argument technique of C++. The only problem is you can specify the default arguments only starting from the right hand side.

void runProgram ( some_type vector , string filename1, string filename2 = "" ) ;

case 1:
   runProgram(vector1, filename, filename2);

case 2:
   runProgram(vector2, filename, filename2);

case 3:
   runProgram(vector3, filename):
 
will solve your purpose.

Cheers!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now