According to the concept of OO, you should NEVER use global static variables. Instead, you can define a static variable in your class for the number of instances of your class. Make it closed so that no one other than your constructor can increase the counter. Provide a public function to get the counter. See the example below:
yourclass.h:
class YourClass {
private:
static int instanceCount_;
public:
YourClass() {++YourClass::instanceCount_;}
~YourClass() {--YourClass::instanceCount_;}
static int instanceCount() {return instanceCount_;}
};
yourclass.cpp:
int YourClass::instanceCount_ = 0;
Regarding the concept of static / global / global static / external 1.static: 1a) global static: Static variable, as shown below:
static int numberOfPersons;
This variable can only be seen in the file (it will not have a collision of names with another name of the same variable in other files)
1a) class static: ( )
, ( Class:: Var) ( " ", ). . ( ).
1b) . ( , "Class::", .
, 1a. 1b. , , er, , . .
- "", :
int numberOfPersons;
"" , "extern". . , .
extern: /, . . 3., , , extern, , , .
extern int numberOfPersons;
int addPersonCount()
{ numberOfPersons ++;
}
, .