Debugging skills in C ++ programming (C ++ programming ideology)

xiaoxiao2021-03-06  68

Debug mark

Applicable preprocessing #define defines one or more debug marks, and manages the debugging section in the code. When the program finally debugged, just use the #undef tag, the debug code will disappear. Commonly used debug markers are Debug, statement sequence:

#define debug

#ifdef debug

Debug code

#ENDIF

2. Debug tag during operation

Turn the debug mark on and off during the program. It can be implemented by setting a debug BOOL tag. This is more convenient to run the command line.

Such as the following code

#include

#include

Using namespace std;

Bool debug = false;

Int main (int Argc, char * argv [])

{

For (int i = 0; i

IF (String (Argv [i]) == "- debug = on")

Debug = True;

BOOL Go = TRUE;

While (Go)

{

IF (debug)

{

Debug code

} else {}

}

}

3. Convert variables and expressions into strings

However, using a string operator to implement the conversion output definition

#define pr (x) cout << # x "=" << x << '/ n'

4.C language assert ()

This macro is in , when using Assert>, give him a parameter, that is, a judgment is a true expression. The preprocessor generates the code that tests the assertion. If the assertion is not true, then an error message tells the assertion and it will fail for a while, and the program will terminate.

#include

Using namsapce std;

int main ()

{

INT i = 100;

Assert (i! = 100); // fails

}

Add #define nDebug before #include to eliminate the code.

}

转载请注明原文地址:https://www.9cbs.com/read-119670.html

New Post(0)