As suggested you have to make the method
static
. That's appropriate for
class methods, that is methods independent on a class instance. E.g.
#include <iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
Point()
{
x = 1;
y = 2;
}
static Point origin()
{
Point p;
p.x = p.y = 0;
return p;
}
void show()
{
cout << "{ x = " << x << ", y = " << y << " }" << endl;
}
};
int main()
{
Point p;
p.show();
p = Point::origin();
p.show();
}
In the above sample code, the
static
method
origin
can be invoked (and, in fact is invoked) without having a
Point
object reference. On the other hand, the standard (instance) method
show
is called having a
Point
object reference.
Note the origin method cannot access any
Point
instance member.