فئة التصحيح في ++Visual C

عند استخدام Debug في تطبيق Visual C++ ، السلوك لا يتغير بين إصدار التصحيح و البناء.

ملاحظات

سلوك Trace يماثل سلوك فئة التصحيح (Debug) ولكن يعتمد على رمز TRACE المعرف. وهذا يعني أنه يجب القيام بـ #ifdef لأية تعليمات برمجية المتعلقة بـ Trace لمنع سلوك التصحيح في إصدار البناء.

المثال

الوصف

النموذج التالي دوماً ينفذ جمل الإخراج بغض النظر عما إذا كنت تترجم برمجياً بـ /DDEBUG أو /DTRACE.

الرمز

// mcpp_debug_class.cpp
// compile with: /clr
#using <system.dll>
using namespace System::Diagnostics;
using namespace System;

int main() {
   Trace::Listeners->Add( gcnew TextWriterTraceListener( Console::Out ) );
   Trace::AutoFlush = true;
   Trace::Indent();
   Trace::WriteLine( "Entering Main" );
   Console::WriteLine( "Hello World." );
   Trace::WriteLine( "Exiting Main" );
   Trace::Unindent();

   Debug::WriteLine("test");
}

الإخراج

    Entering Main
Hello World.
    Exiting Main
test

المثال

الوصف

للحصول على السلوك المتوقع (أي، لا إخراج "اختبار" تمت طباعته لإصدار بناء) ، يجب عليك استخدام توجيهات #ifdef و #endif. تم تعديل نموذج التعليمات البرمجية السابق أدناه لتوضيح هذا الإصلاح:

الرمز

// mcpp_debug_class2.cpp
// compile with: /clr
#using <system.dll>
using namespace System::Diagnostics;
using namespace System;

int main() {
   Trace::Listeners->Add( gcnew TextWriterTraceListener( Console::Out ) );
   Trace::AutoFlush = true;
   Trace::Indent();

#ifdef TRACE   // checks for a debug build
   Trace::WriteLine( "Entering Main" );
   Console::WriteLine( "Hello World." );
   Trace::WriteLine( "Exiting Main" );
#endif
   Trace::Unindent();

#ifdef DEBUG   // checks for a debug build
   Debug::WriteLine("test");
#endif   //ends the conditional block
}

راجع أيضًا:

موارد أخرى

دليل البرمجة لـ NET.