in some cases, when writing C ++ classes, you want to use static initialize functions to initialization is performed on the f66; "> static member. For example, STD: MAP member or STD :: Add fixed content to the vector member. This can be easily implemented through static blocks in Java. But what should I do in C ++?
If you wantInitializationA commonStaticYou only need to define the member in the implementation file (source file) and assign the initial value to it. For example:
Class Test1 {
Public :
Static String Emptystring;
};
string test1 :: emptystring = " " ;
// also can be
// string test1: emptystring;
/// string test1: emptystring (" ");
StaticFunctions cannot be called directly like this. However, use C ++InitializationCommon member features to callStaticInitializationFunction. Of course, this requires defining an additionalStaticMember variables to help. For example:
Class Test2 {
Public :
Static
Vector < String > Stringlist;
Private :
Static Bool _ Init;
Static Bool Init (){
Stringlist. push_back ( " String1 " );
Stringlist. push_back ( " String2 " );
Stringlist. push_back ( " String3 " );
Return True;
}
};
Vector<String>Test2: stringlist;
BoolTest2 ::__ init=Test2: Init ();
In the above exampleStaticMember _ init is called by the way.StaticInitializationThe init () function achieves the intended purpose.
Project Example:
# Include "stdafx. H"
# Include "ctrackview. H"
# Include "databaseinfo. H"
Cvss_rect ctrackview: m_worldrt (0, 0, 0 );
Crect ctrackview: m_screenrt (0, 0, 0, 0 );
HDC ctrackview: m_hdc = NULL;
V_vnopoint ctrackview: v_vnopt;
Ctrackview: ctrackview (void)
{
}
Ctrackview ::~ Ctrackview (void)
{
}
.......