iphone - 可变列表参数

标签 iphone c++ objective-c c

假设我有两个函数

void f1(int p1, int v1, ...);

void f2(int v1, ...);

在 f1 中,我想将可变参数列表中的所有参数传递给 f2:

void f1(int p1, int v1, ...) {
   f2(/*pass all variadic parameters*/);
}

例如,当我调用 f1(1, 2, 3, 4, 5) 时,我想将 2,3,4,5 传递给 f2

最佳答案

尽管您已经接受了正确答案。

如果您无权访问 f2,另一种方法是使用可变参数宏。这是自 C99 以来可用的东西:

#define F1(P1, V1, ...)           \
do {                              \
   /* do something with P1, V1 */ \
   f2(__VA_ARGS__);               \
} while(0)

这里的 do-while 只是封装您的语句的一个技巧,这样对该宏的调用可以出现在普通语句和所有 的任何地方\ 帮助在多行上编写宏。

此类宏非常常用于增强 printf 以打印带有前缀的日志消息,例如:

#define dprintf(...)                               \
do {                                               \
   fprintf(stderr, "%lX: ", (long unsigned)mynum); \
   fprintf(stderr, __VA_ARGS__);                   \
} while(0)

关于iphone - 可变列表参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5283378/

相关文章:

ios - 为什么我每次需要使用它时都必须实例化 appdelegate?

iphone - 在 Xcode 4.2 的 UIWebView 中加载网页——如何正确连接 IBOutlets 并确保网页加载?

c++ - 从 BST 的给定范围插入元素到数组中

c++ - 分配用 new 声明的 vector 中的元素。 C++

c++ - 为什么使用 std::mutex 的函数会对 pthread_key_create 的地址进行空检查?

ios - 基于内容的动态 UITableView 单元格高度

objective-c - cocoa 和OpenGL : failed to draw cube

iphone - “NSString”可能无法响应 '-hexIntValue' 警告,功能正常

iphone - UIImagePickerController 不会停止定位服务

ios - 方法未被调用 : - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken