A static function is a member function of a class that can be called even when an object of the class is not initialized. A static function cannot access any variable of its class except for static variables.
The 'this' pointer points to the object that invokes the function.
a regular function can access static variables but a static function cannot access regular variables
Illustration:
Suppose you have a class named 'A' with two functions fun1 (regular) and fun2 (static).
#include
using namespace std;
class A
{
static int a;
int b;
public:
A(int a, int b) // Constructor
{
this->a = a; // this. points to the variable of
this->b = b; // class A while is a formal parameter
The 'static' keyword can be used in two different ways. I have to agree with Peter that you should actually distinguish between functions and methods. For free-standing functions, i.e. not in the scope of a class, 'static' means that the function should be inlined by the compiler if possible. In any case the function will be local to the compilation unit (most of the time this is a single cpp-file). This means that your function definition is not visible to other compilation units at link time.
However, in the context of methods 'static' has a completely different meaning. Only in this case it does make sense to talk about the 'this' pointer. The 'this' pointer always points to the current instance of a class, also called an object. It is only available inside of regular member functions (also called methods). These methods can only be called on an object:
class A {
public:
void foo() {}
};
...
A a;
a.foo();
If you write 'static' in front of a method it will become a class method. Class methods are not related to the object anymore, but to the class. Because there is no object attributed to a class method call the 'this' pointer is not available in this context. You don't even need an object to call class methods:
class B {
public: static void bar() {}
};
...
B::bar(); // call the method directly
B b;
b.bar(); // also works, but is no different from the previous call
Class methods mostly behave like regular functions declared outside of classes. There is one difference though: You can have 'protected' or 'private' class members (i.e. member variables declared as 'static') which can only be accessed by regular methods and class methods. So, these class member variables are similar to global data, but they are only accessible form functions declared inside the scope of the class.
@Peter: 'static' functions are still allowed in C++. The 'private' keyword is only valid inside of classes/structs. I guess this is to maintain some compatibility to old C code. Unfortunately, 'static' now has two different meanings dependent on the context.
static function/variables in C++ Class can be accessed with out needing to instantiate an element from the Class. The this pointer point to the object it self. On common example usage of the this is when you want to return the reference of the object.
Please, before posting such a question here, did you google "what is a static function in C++" and "this pointer in C++" Or search Stackoverflow with those questions?
Or if you want to know something more specific, don't make us guess - edit your question to be more specific.
I don't think individuals on RG should be taking time to answer basic questions that have extensive and easily found answers on the internet.
Since you are asking about static functions and the this pointer, I'm going to assume that you mean static *methods* (i.e., member functions of a class).
Most object-oriented languages support two kinds of methods:
1) Instance methods are invoked on an instance of a class (i.e., an object). For example, the length() method in class string is an instance method, so it must be invoked on a string object:
Static functions in C++ are functions that are not associated with any particular object. They can be called without creating an object of the class to which they belong. Static functions are useful for functions that do not need to access any data members of the class. They are also useful for functions that need to be called before or after any objects of the class are created.
This pointer in C++ is a hidden pointer that refers to the current object. It can be used to access data members of the current object. This pointer is automatically passed to all member functions of a class.
Static functions and this pointer are two important concepts in C++. Static functions are not associated with any particular object, and can be called without creating an object of the class to which they belong. This makes them useful for functions that do not need to access any data members of the class. They are also useful for functions that need to be called before or after any objects of the class are created.
This pointer, on the other hand, is a hidden pointer that refers to the current object. It can be used to access data members of the current object. This pointer is automatically passed to all member functions of a class.