Nested Comments in C++

后端 未结 4 1285
南旧
南旧 2020-12-31 17:43

This should be a common problem and possibly similar to some question here but i am looking foe the best way to comment out multiple lines (rather methods ) in C++ which hav

4条回答
  •  被撕碎了的回忆
    2020-12-31 18:43

    The stuff between the #if 0 and #endif will be ignored by the compiler. (Your preprocessor might actually strip it out before the "compiler" can even take a look at it!)

    #if 0
    
        /* 42 is the answer. */
    
        Have you tried jQuery?
    
        @Compiler Stop ignoring me!!
    
    #endif
    

    You'll have better control if you use #ifdefs:

    // #define DEBUG
    
    
    #ifdef DEBUG
       MyFunction();
       std::cout << "DEBUG is defined!";
    #endif
    
    
    // Later in your code...
    
    #ifdef DEBUG
        std::cout << "DEBUG is still defined!";
    #endif
    

    Just uncomment the first line, and your #ifdef DEBUG code will suddenly be visible to the compiler.


    P.S. This should clear any more confusion:

    /*
        cout << "a";
        /*
            cout << "b";
        */
        cout << "c";
    */
    

    The output should be "c", assuming your compiler doesn't give you any errors for the last */.

提交回复
热议问题