Just imagine we have the following class:

class A
{
private:
static int m_a;
public:
A() {}
static int get_sum(int b);
};

int A::m_a = 5;

int A::get_sum(int b)
{
return m_a + b;
}

int main() {
    // your code goes here
    A a;
    int c = a.get_sum(10);
    cout << "C=: " << c << endl;
    return 0;
}

In the code above, we have class which contains one private, static member variable which called into our public, static member function get_sum(). Now the question: How the function which has not "this" pointer can access class member variable m_a ? In the Lipman's book I have read that:

(( Point3d* ) 0 )->object_count();

where object_count() does nothing more than return the _object_count static data member. How did this idiom evolve ? ..............................
..............................
//internal transformation of call

object_count(( Point3d* ) 0 );

The language solution was the introduction of static member functions within the official cfront Release 2.0. The primary characteristic of a static member function is that it is without a this pointer.

I don't understand how we can cast 0 to class type object ?

1

There are 1 answers

1
ravi On

Static member functions don't have this pointer and static data member is class-specific rather than object-specific. So, static member functions can access static member variables.