【发布时间】:2012-05-14 11:32:52
【问题描述】:
我正在尝试创建一个通用函数来显示错误消息,并且程序可能会在消息显示后退出。
我希望函数显示发生错误的源文件和行。
参数列表:
1.char *desc //description of the error
2.char *file_name //source file from which the function has been called
3.u_int line //line at which the function has been called
4.bool bexit=false //true if the program should exit after displaying the error
5.int code=0 //exit code
由于 (4) 和 (5) 我需要在函数定义中使用默认参数,因为我不希望它们被指定,除非程序应该退出。
由于 (2) 和 (3),我需要使用一个重定向到原始函数的宏,例如:
#define Error(desc, ???) _Error(desc,__FILE,__LINE__, ???)
问题是我不明白这两个元素应该如何协同工作。
外观示例:
if(!RegisterClassEx(&wndcls))
Error("Failed to register class",true,1); //displays the error and exits with exit code 1
if(!p)
Error("Invalid pointer"); //displays the error and continues
【问题讨论】:
-
在 VA_ARGS 之前添加 '##' 确实有效,我之前尝试过使用可变参数宏,但没有 ## 它不起作用,谢谢
标签: c++ macros mingw default-arguments