Friday, October 14, 2011

C++ Tutorial : This pointer and static members

This Pointer
The keyword this represents a pointer to the object whose member function is being executed. It is a pointer to the object itself.
One of its uses can be to check if a parameter passed to a member function is the object itself. For example,
// this
#include <iostream>
using namespace std;

class CDummy {
public:
int isitme (CDummy& param);
};

int CDummy::isitme (CDummy& param)
{
if (&param == this) return true;
else return false;
}

int main () {
CDummy a;
CDummy* b = &a;
if ( b->isitme(a) )
cout << "yes, &a is b";
return 0;
}



It is also frequently used in operator= member functions that return objects by reference (avoiding the use of temporary objects). Following with the vector's examples seen before we could have written an operator= function similar to this one:
CVector& CVector::operator= (const CVector& param)
{
x=param.x;
y=param.y;
return *this;
}
In fact this function is very similar to the code that the compiler generates implicitly for this class if we do not include an operator= member function to copy objects of this class.
Static Member

A class can contain static members, either data or functions.
Static data members of a class are also known as "class variables", because there is only one unique value for all the objects of that same class. Their content is not different from one object of this class to another.
For example, it may be used for a variable within a class that can contain a counter with the number of objects of that class that are currently allocated, as in the following example:
// static members in classes
#include <iostream>
using namespace std;

class CDummy {
public:
static int n;
CDummy () { n++; };
~CDummy () { n--; };
};

int CDummy::n=0;

int main () {
CDummy a;
CDummy b[5];
CDummy * c = new CDummy;
cout << a.n << endl;
delete c;
cout << CDummy::n << endl;
return 0;
}

In fact, static members have the same properties as global variables but they enjoy class scope. For that reason, and to avoid them to be declared several times, we can only include the prototype (its declaration) in the class declaration but not its definition (its initialization). In order to initialize a static data-member we must include a formal definition outside the class, in the global scope, as in the previous example:
int CDummy::n=0;
Because it is a unique variable value for all the objects of the same class, it can be referred to as a member of any object of that class or even directly by the class name (of course this is only valid for static members):
cout << a.n;
cout << CDummy::n; 
These two calls included in the previous example are referring to the same variable: the static variable n within class CDummy shared by all objects of this class.
Once again, I remind you that in fact it is a global variable. The only difference is its name and possible access restrictions outside its class.
Just as we may include static data within a class, we can also include static functions. They represent the same: they are global functions that are called as if they were object members of a given class. They can only refer to static data, in no case to non-static members of the class, as well as they do not allow the use of the keyword this, since it makes reference to an object pointer and these functions in fact are not members of any object but direct members of the class.

No comments:

Post a Comment