The definition of static member variables consists of two parts.
Like a function, you must first declare it in the class, and then find another place to implement it. Static members must first declare it in the class, and then "IMPLEMENT" outside the class.
This initialization method is determined by the public nature of static members. It cannot be put in the constructor and initialized by every object. It must be implemented outside the class.
// Trace. h
# Pragma once
# Include <string>
Using std: string;
Class CTrace
{
Public:
CTrace (void );
CTrace (const string & name );
~ CTrace (void );
Void debug (const string & msg );
Static bool traceIsActive;
Private:
String theFunctionName;
};
// Trace. cpp
# Include <iostream>
# Include "Trace. h"
Bool CTrace: traceIsActive = false; // This sentence must be added; otherwise, the link is incorrect.
CTrace: CTrace (void)
{
}
Inline ctrace: ctrace (const string & name): thefunctionname (name)
{
If (traceisactive)
{
STD: cout <"Enter function" <name <STD: Endl;
}
}
Ctrace ::~ Ctrace (void)
{
If (ctrace: traceisactive)
{
STD: cout <"exit function" <thefunctionname <STD: Endl;
}
}
Inline void ctrace: Debug (const string & MSG)
{
If (ctrace: traceisactive)
{
STD: cout <MSG <STD: Endl;
}
}