作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
让我用下面的 C++/MFC 代码解释一下我的意思:
static CString MyFormat(LPCTSTR pszFormat, ...)
{
CString s;
va_list argList;
va_start( argList, pszFormat );
s.FormatV(pszFormat, argList);
va_end( argList );
return s;
}
static CString MyFormat2(int arg1, LPCTSTR pszFormat, ...)
{
if(arg1 == 1)
{
//How to call MyFormat() from here?
return MyFormat(pszFormat, ...); //???
}
//Do other processing ...
}
如何从 MyFormat2()
中调用 MyFormat()
?
最佳答案
你不能直接这样做:一旦你打开 va_list
,你就不能将它传递给一个接受 ...
的函数,只能传递给一个接受 的函数>va_list
.
这不会阻止您在采用可变参数列表的多个函数之间共享可变参数代码:您可以遵循 printf
+ vprintf
的模式,提供采用 va_list
的重载,并从两个地方调用它:
public:
static CString MyFormat(LPCTSTR pszFormat, ...) {
// Open va_list, and call MyFormatImpl
}
static CString MyFormat2(int arg1, LPCTSTR pszFormat, ...) {
// Open va_list, and call MyFormatImpl
}
private:
static CString MyFormatImpl(LPCTSTR pszFormat, va_list args) {
// Implementation of the common functionality
}
关于c++ - 如何从类似函数调用具有可变数量参数的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22123355/
我是一名优秀的程序员,十分优秀!