Reputation: 4047
I am looking for information about static methods in C++. I searched but honestly was not able to clearly understand a thing.
Are static functions those functions which contain static data members only?
Upvotes: 7
Views: 26204
Reputation: 17643
Static methods in a C++ class, like those for a Java class, are methods that are available without having to actually instantiate an object or an instance of the class.
The only way to use the standard, non-static methods of a class is to create an object or instantiate an instance of the class. Then when you use the methods you are operating on a particular object or instance of the class.
There are some restrictions on a static method. For instance, you can not use the this
pointer in a static method. This is because a static method of a class is not associated with a particular, specific object. It is instead a general method that is not tied to any particular object.
The way I think of a static method in a class is that when I do so I am creating a particular namespace, the class name, and then adding a function that can only be accessed by using that particular namespace.
A static variable within a class is created at application startup and can be accessed without having a particular instance of the class created. A static variable is also shared by all instances of the class.
So for an example of the differences (off the cuff so there may be compile errors):
class myAclass {
public:
myAclass(); // constructor
void function1 (int iValueSet); // a standard method
static void functionStatic (int iValueSet); // a static method
private:
int iValue; // an object specific variable
static int iValueStatic; // a class general variable shared by all instances of this class
};
int myAclass::iValueStatic = 0; // init the class static variable
myAclass::myAclass () : iValue (0)
{
}
void myAclass::function1 (int iValueSet)
{
iValue = iValueSet; // set the iValue for this particular object
iValueStatic = iValueSet; // set the shared iValueStatic for all instances
}
void myAclass::functionStatic (int iValueSet)
{
// iValue = iValueSet; // ERROR this is not allowed as iValue is not static
iValueStatic = iValueSet; // works since iValueStatic is static
}
And then how this class could be used:
myAclass jj; // create an object instance
jj.function1(5); // access the non-static method to change the object data
myAclass::functionStatic(8); // set the static iValueStatic
And of course since struct is similar to class except that struct members are public by default, this works for struct as well.
One reason for using static functions and variables is to create an object factory for the class using the factory pattern. Another use is with the Singleton Pattern.
Upvotes: 13
Reputation: 6049
Static methods of a class have no this
pointer. That means they cannot access instance member data. Methods (static or otherwise) do not contain data members. (They can declare variables on the stack or heap, however.)
Static methods are usually called with the class name (myClass::foo()
) since you don't have to declare an instance of the class to use them, but can be called with the instance (myInstance.foo()
) also.
Upvotes: 5