Thursday, March 24, 2011

Difference between static in C and static in C++??

Difference between static in C and static in C++??
http://stackoverflow.com/questions/943280/difference-between-static-in-c-and-static-in-c

C++ has one more use, static within a class. When used there, it becomes a single class variable that's common across all objects of that class. One classic example is to store the number of objects that have been instantiated for a given class.

class Plop
{
static int x; // This is a member of the class not an instance.

public:
static int getX() // mehtod is a member of the class.
{
return x;
}
};

int Plop::x = 5;






________________
The use of static at the file scope to restrict access to the current translation unit is deprecated in C++, but still acceptable in C.

Instead, use an unnamed namespace




----------------
Note that the use of static to mean "file scope" (aka namespace scope) is only deoprecated by the C++ Standard for objects, not for functions. In other words,:

To quote Annex D of the Standard:

The use of the static keyword is deprecated when declaring objects in namespace scope.

No comments:

Post a Comment