开发者

C++ preprocessor directive

I have a macro which defines the model number of an equipment. I am having problems determining how to compare it to a string.

In a customer's specific header I have defined my macro as follows:

#define FTP_MODEL_NUM CT1030

Here I want to conditionally compile a section of code depending on the model number but no matter what value my macro has it compiles it anyway:

#if FTP_MODEL_NUM == CT1031  
    QMessageBox * lolers=new QMessageBox;  
    lolers->setWindowTitle(tr("title"));  
    lolers->setText(tr("this is test"));  
    lolers->s开发者_运维知识库how();  
#endif  

What am I missing? Do I absolutely need to compare it to another macro when using the == operator? I'm using Qt on Linux.


Assuming C++'s preprocessor works the same way as C99's, what you're trying to do can't work. After FTP_MODEL_NUM == CT1031 is expanded to, e.g., CT1030 == CT1031, any identifiers remaining in the expression are replaced with 0, yielding 0 == 0, which is always true. I believe the standard way to do what you're trying to do is to define a macro with the same name as the model number (e.g., #define CT1030) and then implement the test with #ifdef CT1031.


You cant lexically compare macros afaik. Only numerically. Define a second macro with numbers and use that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜