Static

Hi All,

In C, when we declare a function as static, we can see it only in the same .c file. The same goes in CPP.
But when we declare a static function in a class, such a pattern is not really helpful.

So what exactly is the difference between a static function and a static function member of a class in terms of : scope, or any other differences

Thx
~Cheers!
Neeraj
A static member function may be accessed without having an instance. It is not passed a *this. A static member function may be accessed from other files, unlike a static function. In other words, although the keyword is the same, they have completely different semantics.
A static member function is a function which is not applied to any object but is still in the class namespace
( You can't access non-static members from a static function because this is not passed to that function )
what exactly is the difference between a static function and a static function member of a class
There hardly is anything similar. Just a bad case of keyword reuse.

When used in namespace scope the keyword static means that the function/variable will have internal linkage. That is, function/variable with the same name in another translation unit will not refer to the same function implementation / variable storage.

When used in class scope with variable, it means that the storage for it will not be associated with particular object. Consequently the data will not be in the memory layout for individual objects, but is shared between all of them and remains at some fixed address.

When used in class scope with function, it means that the function will only access static class variables (and whatever other data namespace scope functions can access) and consequently can be used without object instances.

Regards

EDIT: Functions and variables in class scope always have external linkage. It is error to provide the static storage class when defining variable or function that is declared inside a class.
Last edited on
Topic archived. No new replies allowed.