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();
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.