1
this Pointer and Static Members
OOP
- this points to the current object. Used when a parameter has the same name as a member, or to return the object itself.
- static data member — one copy shared by every object, e.g. a counter of objects.
- static member function — called with the class name, can use only static members (no
this).
class Student {
string name;
static int count; // shared
public:
Student(string name) {
this->name = name; // this-> separates member from parameter
count++;
}
static int total() { return count; }
};
int Student::count = 0; // define outside the class
Student a("A"), b("B");
cout << Student::total(); // 2