Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

static void function

Status
Not open for further replies.

searchgo

Programmer
Jun 20, 2003
1
0
0
CA
I wonder what's mean static function in C? Or what is the difference between static and non-static function in C language.

Thanks!
 
1) In C static functions are only visible within the source in which they are defined. They are not globally visible.

eg
File A.c
static eh()
{
}

File B.c
cannot see eh

2) In C++, a static function with global scope is the same as (1).

3) In C++, a static function in a class is accessible without an object pointer. The visibility depends on whether it is public, protected or private.
 
Also in C++, if the static member is part of a class, each reference to the static member (dispite which instance of the class) still refers to the same peice of memory... This has a number of uses: from singleton factory classes, to simply keeping a count of how many instances their are of a single class.
 
Hi,
Static member function and member variables are class attributes .. and others are object attributes. In other words, say,In class C1, function F1 and variable V1 are static and function F2 and variable V2 are normal i.e.auto. Then,
1. Function F1 can use variable V1 but not V2.
2. Function F2 can use variables V1 and V2.
3. Function F1 and variable V1 has to accessed using class like C1::F1() and C1::V1.
4. Function F2 and variable V2 has to be accessed using object like O1.F2() and O1.V2, where O1 is C1 instance.

Static functions and variables are normally used in singleton class where constructor is made private. For eg.

class mySingleton{
public:
static mySingleton * getSingletonPtr();
private:
mySingleton();
static mySingleton * mySingletonPtr;
};

Now, get the pointer to my singleton class as .....
mySingletonPtr *ptr = mySingleTon::getSingletonPtr();

hope this helps.
regards,
Mahesh
 
xwb gives you the appropriate answer in context of "C". When you make your functions static,the notion is basically that you want this function to not to be shared across multiple files in the same compilation unit. It's just limited to a single file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top