C++基礎:有趣的#define的一個實例

字號:

看了一下google CoverStory的代碼,有一個地方很有意思:
    // These are the various document types used by CoverStory.
    // Included in both Obj-C and plist sources.
    // A little magic to get CPP to turn things into cstrings for the code but have
    // the strings raw for the plists. The reason we need this is if we just put
    // them in quotes (cstrings) then we’d get those quotes in the plists, which we
    // don’t want.
    #define STRINGIFY(x) #x
    #define TO_STRING(x) STRINGIFY(x)
    #define kGCOVTypeNameRaw GNU Compiler Coverage File
    #define kGCOVTypeName TO_STRING(kGCOVTypeNameRaw)
    然后在使用的地方
    NSLog(@kGCOVTypeName);
    或者
    std::cout<    就能把GNU Compiler Coverage File考試,大提示值輸出來了,
    但如果用NSLog(@#kGCOVTypeNameRaw);則不行。