调试宏与调试变量

use*_*089 0 c++ debugging macros

下面是使用调试变量的示例

class A{
 public:
  A(bool debug):m_debug(debug){};
  ~A(){};

  void Test(){
    for(int i=0;i<1000000;i++){
      // do something

      if(m_debug) print();
    }
  }

  void print(){
    std::cout << "something" << std::endl;
  }


 private:
  bool m_debug;
};
Run Code Online (Sandbox Code Playgroud)

下面是使用调试宏预处理器的示例

#include "Preprocessor.h"

class A{
 public:
  void Test(){
    for(int i=0;i<1000000;i++){
      // do something

#ifdef DEBUG
        print();
#endif
    }
  }

  void print(){
    std::cout << "something" << std::endl;
  }
};
Run Code Online (Sandbox Code Playgroud)

在Preprocessor.h中简单

#define DEBUG
Run Code Online (Sandbox Code Playgroud)

使用调试变量的好处是类对全局预处理器头的依赖性较小.关于宏方法的好处是,如果在运行时执行语句,则减少1000000,这对于每个fps计数时的图形应用程序来说可能是至关重要的.什么被认为是更好的方法?

小智 5

更好的方法是使用预处理器,但是,没有必要使用新的头文件来定义宏.

您可以在编译时设置标志,使用-DMACRO_NAME或在您的情况下-DDEBUG.

  • 或者OP的编译器的任何适当的命令行语法. (3认同)