In Java
we have variable arguments
This syntax tells the compiler that fun()
can be called with zero or more arguments. As a result, here a is implicitly declared as an array of type int[]
.
class Test1
{
// A method that takes variable number of integer
// arguments.
static void fun(int ...a)
{
System.out.println("Number of arguments: " + a.length);
// using for each loop to display contents of a
for (int i: a)
System.out.print(i + " ");
System.out.println();
}
// Driver code
public static void main(String args[])
{
// Calling the varargs method with different number
// of parameters
fun(100); // one parameter
fun(1, 2, 3, 4); // four parameters
fun(); // no parameter
}
}
I am looking to implement the same thing in C++
but I couldn't find anything useful. Does anyone have any ideas?