首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

定义不定参数的函数的有关问题!

2012-02-11 
定义不定参数的函数的问题!!!如何实现如printf(%d %s,10,1000)的函数?参数个数是不定的,参数类型也是

定义不定参数的函数的问题!!!
如何实现如printf("%d %s",10,"1000");的函数?
参数个数是不定的,参数类型也是不定的?不考虑重载
谢谢!!!

[解决办法]
...
A partial parameter list can be terminated by the ellipsis notation, a comma followed by three periods (, ...), to indicate that there may be more arguments passed to the function, but no more information is given about them.
[解决办法]
void MyPrint(char *format,...)
{
char szVarBuffer[VAR_STRING_BUFFER_SIZE];
va_list ar_ptr;
va_start(ar_ptr,format);
vsprintf(szVarBuffer,format,ar_ptr);
va_end(ar_ptr);

cout<<szVarBuffer<<endl;
}
[解决办法]

C/C++ code
void Print(const char* pText, ...){    char buf[1024] = {0};    va_list  va;    va_start (va, pText);    _vsnprintf(buf + strlen(buf), sizeof(buf) - 1, pText, va);    va_end(va);    printf(buf);} 

热点排行