6

I got a little confused of the usage of static / global / global static / extern variables.

I would like a counter variable to get increased in any creation of a class instance.

Would highly appreciate if someone can post a clarification of the appropriate usage for each.

yael aviv
  • 201
  • 1
  • 5
  • 9
  • The first two paragraphs seem unrelated ? You would just need a `static` counter variable in your class which gets incremented in your constructor(s) and decremented in your destructor. – Paul R Sep 16 '14 at 11:01
  • @PaulR, why not global? – yael aviv Sep 16 '14 at 11:05
  • Duplicate question. http://stackoverflow.com/questions/15235526/the-static-keyword-and-its-various-uses-in-c and http://stackoverflow.com/questions/10422034/when-to-use-extern-in-c – Sam Varshavchik Sep 16 '14 at 11:06
  • A static class variable is somewhat like a global, in that there is one instance and it has the same lifetime as the program. – Paul R Sep 16 '14 at 11:06
  • @PaulR, LOL! This is what get me confused.... – yael aviv Sep 16 '14 at 11:09

1 Answers1

5

According to OO concept, you should NEVER use global static variables. You can instead define a static variable in your class for the instance count of your class. Make it private, so that no one else except your constructor can increase the count. Provide a public function to get the counter. See example below:

yourclass.h:

class YourClass {
private:
    static int instanceCount_;
public:
    YourClass() {++YourClass::instanceCount_;}  // constructor
    ~YourClass() {--YourClass::instanceCount_;} // destructor
    static int instanceCount() {return instanceCount_;}
};

yourclass.cpp:

int YourClass::instanceCount_ = 0;

As far as the concept of static / global / global static / extern

  1. static:

1a) global static: A static variable created as given: static int numberOfPersons;

This kind of variable can only be seen in a file (will not have name collision with other variable having same name in other files)

1b) class static: (already has an example in the instance count above) A class may have static members, which are visible to that class (accessed only by Class::VarName syntax) only (instead of 'file only' as said above). It will not have name collision with variables of same name in other class. It only has one copy per class (not per instance).

1c) Both global static and class static are global(since they can be globally accessed, either with class qualifier Class:: or not.

So, 1a., 1b. and 1c. partially explain static, global, and global static

  1. Another form of global variable, is just define a variable without static
    syntax: int numberOfPersons;

Without static, this variable can be seen by other file, using extern keyword. And it will have name collision with the variables having same name in other files. So, globally, you can only define it ONCE across all your source files.

  1. extern: Declare a variable/function which is defined in somewhere else. It is normally seen in a header file. We can have some variables defined in other file, and declare this variable as extern, like below, in another source file which uses it.
extern int numberOfPersons;

    int addPersonCount()
    {
         numberOfPersons++;
    }

Hope this helps.

user18520267
  • 180
  • 3
  • 13
Robin Hsu
  • 4,164
  • 3
  • 20
  • 37
  • @RoninHsh, Thanks. What does the `inline` mean? – yael aviv Sep 16 '14 at 11:18
  • 1
    Don't forget the copy constructor and move constructor. Maybe not relevant here, but the use of an atomic may be better for thread safety. – Niall Sep 16 '14 at 11:21
  • 1
    But C++ is not an OO language; it is multi-paradigm. Thus not only is this answer woefully incomplete but it may also be said to miss the point of the question entirely. – Lightness Races in Orbit Sep 16 '14 at 12:59
  • Sorry, forget to mention the destructor, to decrement the count. – Robin Hsu Sep 17 '14 at 01:37
  • Updated. Anything defined in header file is already inline. Thus the `inline` is redundant, and is removed. `inline` means optimized it as if it is written in the context of the caller of the function, without really calls it. (`inline` to the place it is called). – Robin Hsu Feb 04 '17 at 09:49