blog
blog copied to clipboard
C++ Instance and Static Members
Reference: Book: Starting Out with C++ from Control Structures to Objects, byTony Gaddis, eighth edition
Concept: Each instance of a class has its own copies of the class's instance variables. If a member variable is declared
static
, however, all instances of that class have access to that variable. If a member function is declaredstatic
, it may be called without any instances of the class being defined.
1. Instance Variables
Each class object (an instance of a class) has it own copy of the class's member variables. An object's member variables are separate and distinct from the member variables of other objects of the same class.
For example, a Rectangle
class has two member variables: width
and length
. Suppose we define two onjects of the Rectangle
class and set their width
and length
member variables as follows:
Rectangle box1, box2;
// Set the width and length for box1.
box1.setWidth(5);
box1.setLength(10);
// Set the width and length for box2.
box1.setWidth(500);
box1.setLength(1000);
This code creates box1
and box2
, which are two distinct objects. Each has its own width
and length
member variables, as illustrated:
When the getWidth
member function is called, it returns the value stored in te calling object's width
member variable.
cout << box1.getWidth() << " " << box2.getWidth() << endl; //5 500
In object-oriented programming, member variables such as the Rectangle
class's width
and length
members are known as instance variables. They are called instance variables because each instance of the class has its own copies of the variables.
2. Static Members
It is possible to create a member variable or member function that does not belong to any instance of a class. Such members are known as static member variables and static member functions.
When a value is stored in a static member variable, it is not stored in an instance of the class. Likewise, static member functions do not operate on instance variables. Instead, they can operate only on static member variables.
You can think of static member variables and static member functions as belonging to the class instead of to an instance of the class.
2.1 Static Member Varibles
When a member variable is declared with the key word static
, there will be only one copy of the member variable in memory, regardless of the number of instances of the class that might exist. A single copy of a class’s static member variable is shared by all instances of the class. For example, the following Tree class uses a static member variable to keep count of the number of instances of the class that are created.
// Tree.h
class Tree
{
private:
static int objectCount; // Static member variable.
public:
// Constructor
Tree()
{ objectCount++; }
// Accessor function for objectCount
int getObjectCount() const
{ return objectCount; }
};
// Definition of the static member variable, written
// outside the class.
// This external definition statement causes the variable
// to be created in memory, and is required.
// We could have left out the initialization because C++ automatically
// stores 0 in all uninitialized static member variables.
int Tree::objectCount = 0;
Next, look at the constructor, the ++
operator is used to increment objectCount
. Each time an instance of the Tree class is created, the constructor will be called, and the objectCount member variable will be incremented. As a result, the objectCount
member variable will contain the number of instances of the Tree class that have been created.
#include <iostream>
#include "Tree.h"
using namespace std;
int main()
{
// Define three Tree object.
Tree oak;
Tree elm;
Tree pine;
// Display the numebr of Tree objects we have.
cout << "We have " << pine.getObjectCount() // 3
<< " trees in our program!\n";
return 0;
}
2. Static Member Functions
static ReturnType FunctionName (ParameterTypeList);
A function that is a static member of a class cannot access any nonstatic member data in its class. The following two points are important for understanding static member functions' usefulness:
- Even though static member variables are declared in a class, they are actually defined outside the class declaration. The lifetime of a class’s static member variable is the lifetime of the program. This means that a class’s static member variables come into existence before any instances of the class are created.
- A class’s static member functions can be called before any instances of the class are created. This means that a class’s static member functions can access the class’s static member variables before any instances of the class are defined in memory. This gives you the ability to create very specialized setup routines for class objects.
-
Declared with static before return type:
static int getObjectCount() { return objectCount; }
-
Static member functions can only access statice member data
-
Can be called independent of objects (instances):
int num = Tree::getObjectCount()
// Modified Version of Tree.h
class Tree
{
private:
static int objectCount; // Static member variable.
public:
// Constructor
Tree()
{ objectCount++; }
// Accessor function for objectCount
static int getObjectCount();
{ return objectCount; }
};
int Tree::objectCount = 0;
cout << "There are " << Tree::getObjectCount() << " objects.\n";