Access to a member of the class. This class object has never been created. All members are of type static.

I have a working code base that has a class called Tabs. All methods and variables of this class are defined as static. I understand that a static member of a class is used by all instances of class objects. This class is used to store some type of data in the form of sets. Many different files use the Tabs :: find () and Tabs :: Insert () member functions without instantiating an object of the Tabs class. I am trying to understand how this works and what is called this programming technique. Thank.

+3
source share
2 answers

staticData items are initialized before they mainenter, causing access to them. They are in static memory, as opposed to dynamic or automatic.

A class with only static elements is similar to global variables and functions, but is grouped. This is not programming technology itself. These are just globals.

//globals.h
class Globals
{
   static int x;
public:
   static int getX() {return x;}
};

//globals.cpp
#include "Globals.h"
int Globals::x = 1;

//main.cpp
#include "Globals.h"
//x is initialized before call to main
int main()
{
    int x = Globals::getX();
}
+4
source

I would call it "obsolete." It essentially uses class(or struct, as the case may be) to emulate a namespace.

class whatever { 
    static int a, b, c;
    static double x, y, z;
};

int whatever::a, whatever::b, whatever::c;
double whatever::x, whatever::y, whatever::z;

Pretty much the same as:

namespace whatever {
    int a, b, c;
    double x, y, z;
}

, , namespace, . , , , , - , namespace ().

. , class/struct, static , , , .

+2

All Articles