Static member functions
can be used withstatic member
variables in the class. An object of the class is not required to call them.
- The
this
pointer does not exist in static member functions because thethis pointer
belongs to the object, whereas static member functions belong to the class, not to the object. In static behavior, we can also call otherstatic data members
andstatic functions
.Static member functions
cannot be constant.
You can call a
static function
by the object name with thememory access operator (.)
, but it is not considered good programming practice. It is better to access thestatic member function
of a class by the class name with thescope resolution operator (::)
.
#include <iostream>
using namespace std;
class Circle
{
int radius;
static const float Pi;
public:
//--------Setter/Mutator------------
void setRadius(int radius)
{
this->radius = radius;
}
int getRadius() const
{
return radius;
}
//-----Getter/Accessor--------------
static int getPi()
{
// Here,you can't access any data member and member function directly except static data member and other static function
// like
// radius=6; -->error
// setRadius(5); -->error
return Pi;
}
};
int main()
{ // Accessing static data member of class through static function with class name and scope resolution operator
cout << Circle::getPi();
}