Static Members and the this keyword in C++ Classes
Object Oriented Programming in C++ – Part 8
Forward: In this part of the series, we look at static members in C++ classes.
By: Chrysanthus Date Published: 23 Aug 2012
Introduction
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
Static Property
A static property is also called a static data member.
Declaring a Static Property
You declare a static property just as you declare any other attribute, but you precede the declaration expression with the keyword, static (and a space). The syntax is:
static Type Ident;
Despite this simple feature, you have to learn how to use the static member. You do not use it in the straightforward way.
Example
The following class illustrates the use of a static property member:
#include <iostream>
using namespace std;
class MyClass
{
public:
static int sameAll;
};
int MyClass::sameAll = 5;
int main()
{
MyClass myObj;
myObj.sameAll = 6;
cout << MyClass::sameAll;
return 0;
}
You instantiate an object from the class that has the static member, in the normal way. Line 1 in the main function illustrates this. You access the static property of an instantiated object in the normal way. The second line in the main function illustrates this. However, changing the value as this line has done means changing the value for the class (description) and any instantiated object and any object that is still to be instantiated.
The third line in the main function displays the static property value. It uses the class name; it did not use the object name. To use the class name to access the static attribute (property), you begin with the class name. This is followed by the scope resolution operator and then the identifier of the static property. This shows how you can access a static attribute with the class name directly and without using an object; this is like accessing the property in the class description. The static member is a kind of global object.
You can still use the instantiated object to access the static property. So, the cout statement could have been:
cout << myObj.sameAll;
where the static property has been accessed in the normal way.
The following example illustrates that the static data member is the same for its class and its instantiated objects and if you change it through an object or through the class (MyClass::sameAll), the value is changed to the same new value, for all the objects and the class:
#include <iostream>
using namespace std;
class MyClass
{
public:
static int sameAll;
};
int MyClass::sameAll = 5;
int main()
{
MyClass::sameAll = 6;
cout << MyClass::sameAll << "\n";
MyClass obj1;
obj1.sameAll = 7;
MyClass obj2;
cout << MyClass::sameAll <<"\n";
cout << obj1.sameAll<<"\n";
cout << obj2.sameAll;
return 0;
}
Read through the code and try it. The first line in main changes the value using the class name. The second line displays the changed value. The third line instantiates an object from the class. The fourth line changes the value using the instantiated object. The fifth line instantiates a new object. The rest of the lines display the value using the class and then the two different objects. These three lines, all display the same value (7) confirming the purpose of static member.
Static Method
You can also have static methods. A static method can only refer to static members. It cannot use the keyword, this (see below). I will not address static methods any further in this article.
The this keyword is not a component of static members. However, I have just included it in this article because it is short. The keyword, this, represents a pointer to the object whose member method is being executed. It is a pointer to the object itself. The following code gives an illustration:
#include <iostream>
using namespace std;
class MyClass
{
public:
int num1;
int assignShow()
{
this->num1 = 5;
cout << num1;
}
};
int main()
{
MyClass myObj;
myObj.assignShow();
return 0;
}
A pointer object holds the address of a pointed object. The pointer is actually the address to the pointed object. The keyword, this, is a reserved word. It is predefined in C++. It is a pointer to the object whose method is being executed. In other words, it is a fundamental object that has the address to the class object whose method is being executed. The, this, keyword has to be coded in a method. Any method has its time of execution.
The pointed object here is the object instantiated from a class, whose method is in the process of execution. As the method is being executed, using the word, this, would refer to the method’s object. That is how the C++ Class was authored.
In the above code, the method assignShow() uses the keyword, this, which is the pointer to its object (when the object is instantiated from the class). Line 1 in the method uses “this” to access the property, num1. It was not very necessary just to assign a value to a property this way, as we could have used num1 directly, because a method can access a property of the same class (object). However, it was done to illustrate the use of the “this” keyword. Note the use of the arrow operator with the “this” keyword. The arrow operator is the operator to use to join a class object pointer and the object property (or method). Read and try the above code.
Well, let us end here and continue in the next part of the series.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT