Static in C++

Hey, its been a really long time.
I couldn't get my adsense account approved.
It seems I need more content.
If only I spend enough time blogging. Everyone gets busy once in a while. And everyone forgets that they have a blog. Especially ones like mine, where people rarely visit.
Anyways, that's relevant to what I wanted to make note of here.
I was working on a minor change in my company's C++ code base.
Its a simple one for anyone who knows C++. But I don't do much C++. The last time I remember writing real C++ code was during my university days. I mean my bachelors degree. During my masters I worked solely on Java based stuff for my projects and for a coursework, I just opted to do something in ASP .NET, just to get a feel of it.
Coming straight back to the point. Static is an overused keyword. Literally it would mean something that doesn't change. But in C++, this keyword has special meanings, depending on the context it is used.
Static can be used for three purposes.
One use is to have a variable defined that has a lifetime of the whole program. That is once you declare it static and initialize it, then that variable's value can be accessed from anywhere in that program. That variable's allocation is cleared up only when the program closes or quits. And it helps prevent re-initialization. If I were to write something like this:

for (...) {
static int etech = 0;
etech++;
}

If the program looped through even a 1000 times, the value of etech would be initialized only once. Static keyword here prevents it from being re-initialized to zero. This is simply because we have already asked it to stay in memory till the end of the program, so no more initializing again. But you can do operations on it like addition, subtraction  and what so ever.

Now what I the use I have mentioned so far is the simplest of uses of the keyword 'static'. I could use it to do more when talking about members of a class.

Usually member variables in a class are meant to be attributes of its objects. Every object oriented programming book teaches us that. Now what if you wanted one member to be shared by all the objects at the same time, without bothering to set it individually for each object? In other words, what if I wanted the class to have a property that are shared by objects. Hence it becomes more like class level property. I don't know if I am supposed to call it that way. But if I have a member variable as follows:
Class A{
private: 
int m_etech=0;
};
That would mean every instance of A would have its on m_etech.

Now if I add the keyword  static to it like below:
Class A{
private: 

static int m_etech=0;
};

Then every instance of A would share this one common m_etech.
In most examples this is used as a way to count the number of instances of a class.

Now over here I have declared it in one file.

Suppose like in most commercial cases, you have a header file and a cxx file.Then to get things compiled, I would have to do the following:

I declare the following in the header A.h:
Class A{
private:
static int m_etech;
};

And in A.cxx I would write the following:

int m_etech=0;

Remember: If you don't initialize that value in your cxx file, you can't compile your code. It would say that the variable is not found.



Another use of static keyword is to have a member function of your class that can be invoked without instantiating the class.
Oh now you need to know about the Scope Resolution Operator which is nothing but '::'.

This is to provide a kind of full name to the variable or member function. Usually used to define member functions in a cxx file.

Now static member functions have a special feature, they can only modify static variables. This is because of the fact that the static member function doesn't really belong to just any instance of the class, but is shared by all instances of the class.



That's all I have for now.
You could google for more information. Or maybe I'll just format it better so that you can read easy.

http://www.cprogramming.com/tutorial/statickeyword.html

Comments