Welcome~~~


Another blog:
http://fun-st.blogspot.com/

It is easier to write an incorrect program than understand a correct one.

Wednesday, February 9, 2011

# What does the 'static' keyword mean in C++

==> The Static keyword in a class::

In C++, a static method is not associated with an instance of the class. Therefore you neither need an instance to call the method nor can the method access any instance data:

class foo
{
public:
int method();
static int smethod();

private:
int field;
};

To call method, you first need an instance of foo:

foo f;
int x = f.method();

You can call smethod without having an instance:

int y = foo::smethod();

Note that you can also call smethod like an instance method but that's just a convenience:

int z = f.smethod();  // legal, but smethod has no access to the f instance
// such as 'field'


Static methods:

  • Provide encapsulation in the "namespace" created by the class. If your class is Animal and the static method is Create, you have to call it with Animal::Create. This is better than global functions, and allow implementing Factories and "virtual constructors" with relatively natural syntax.
  • Have access to static members. Such members are useful in some cases, and without static methods and members you would have to use global variables and functions.

==> A Static function in header file::

Is the function defined in the header file? So that the actual code is given directly in the function, like this:

static int addTwo(int x)
{
 
return x + 2;
}

Then that's just a way of providing a useful function to many different C files. Each C file that includes the header will get its own definition that it can call. This of course wastes memory, and is (in my opinion) a quite ugly thing to be doing, since having executable code in a header is generally not a good idea.

Remember that #include:ing a header basically just pastes the contents of the header (and any other headers included by it) into the C file as seen by the compiler. The compiler never knows that the one particular function definition came from a header file.

It will effectively create a separate static function with the same name inside every cpp file it is included into. The same applies to global variables.


==> A Static variable::
First, static is not a generic OOP term. it is language specific. So lets focus and talk about static in C++
First thing first, know that the static word has first appeared in C, and had kept it original meaning, when you place it outside of a class.
C static examples:
First meaning, something like global variable in a local scope:
void foo() {
   static int counter = 0 ;
   counter++;
}
the counter variable life is global - each call will increment it (call foo() three times, and it's value would be 3). But, it has a local scope - no other function (except foo) can access it.


==> Compare of Static function and Friend function::

Static functions are used when you want a function that is the same for every instance of a class. Such functions do not have access to "this" pointer and thus cannot access any non static fields. They are used often when you want a function that can be used without instantiating the class.

Friend functions are functions which are not in the class and you want to give them access to private members of your class.

And this(static vs. friend) is not a matter of using one vs the other since they are not opposites.

A static function is a function that does not have access to this.

A friend function is a function that can access private members of the class.




♥ ¸¸.•*¨*•♫♪♪♫•*¨*•.¸¸♥
http://stackoverflow.com/questions/3090994/what-does-the-static-keyword-mean-in-oop
http://stackoverflow.com/questions/780730/c-c-static-function-in-header-file-what-does-it-mean
http://stackoverflow.com/questions/4921150/when-to-use-static-member-function/4921310#4921310
http://stackoverflow.com/questions/2315166/where-would-you-use-a-friend-function-vs-a-static-function

No comments:

Post a Comment