C++ 标准库,可变参数数量,参数类型相同

#include <iostream>
#include <stdarg.h>

// 可变参数数量,但是类型要相同。
//

int test(const int num_of_inputs, ...)
{
	int result(0);
	va_list parlist;
	va_start(parlist, num_of_inputs);
	for (int i = 0; i < num_of_inputs; ++i)
	{
		std::cout << va_arg(parlist, int) << std::endl;
	}
	va_end(parlist);
	return result;
}

//////////////////////////////////////////////////////////

int main()
{
	test(0,2,3.6,4);	// error, different types
	test(0, 2, 3, 4);	// ok
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/alexYuin/p/11967748.html