In C++ static methods and fields are elements that do not belong to a particular instance but belong directly to the class itself. This means that you can access them without needing to create an object of the class.
Static methods and fields have their advantages, but also their disadvantages. If you want to learn more about them, and when to use them and when not to, read the article I provided.
If you want to learn more about Static Methods and Variables
check out the Object-Oriented Programming Course read more ⯈
Static Methods
Static methods in C++ are defined using the static
keyword. These methods do not have access to non-static members of the class and can only access other static methods and fields.
The syntax to declare a static method is as follows:
class MyClass
{
public:
static void StaticMethod()
{
// Static method code
}
};
Example of a Static Method
Here’s a simple example of a static method that performs a mathematical operation without needing to create an instance of the class:
#include <iostream>
class Util
{
public:
static int Add(int a, int b)
{
return a + b;
}
};
int main()
{
int result = Util::Add(5, 3);
std::cout << "Result: " << result << std::endl; // Output: Result: 8
return 0;
}
In this example, Add
is a static method of the Util
class. You can call it directly using the class name Util
, without needing to instantiate it.
Static Fields
A static field (or static member variable) is a variable that is shared by all instances of a class. Unlike non-static fields, which have a distinct value for each instance, static fields have a single shared value.
The syntax to declare a static field is as follows:
class MyClass
{
public:
static int StaticField;
};
Then, you must define the static field outside the class:
int MyClass::StaticField = 0;
Example of a Static Field
In the following example, we show how a static field can be used to keep a shared count among all instances of a class:
#include <iostream>
class Counter
{
public:
static int Count;
Counter()
{
Count++;
}
};
// Definition of the static field
int Counter::Count = 0;
int main()
{
Counter c1;
Counter c2;
Counter c3;
std::cout << "Total instances created: " << Counter::Count << std::endl; // Output: Total instances created: 3
return 0;
}
In this example,
Count
is a static field that increments each time a new instance of theCounter
class is created.- The value of
Count
is shared by all instances ofCounter
.
Practical Example
Let’s create a class that combines static methods and fields to illustrate how they can be used together. In this case, we will use a Calculator
class that counts the total number of operations performed:
#include <iostream>
class Calculator
{
public:
static int TotalOperations;
static int Add(int a, int b)
{
TotalOperations++;
return a + b;
}
static int Subtract(int a, int b)
{
TotalOperations++;
return a - b;
}
static int Multiply(int a, int b)
{
TotalOperations++;
return a * b;
}
static int Divide(int a, int b)
{
if (b != 0)
{
TotalOperations++;
return a / b;
}
else
{
std::cerr << "Error: Division by zero" << std::endl;
return 0;
}
}
};
// Initialization of the static field
int Calculator::TotalOperations = 0;
int main()
{
int sum = Calculator::Add(10, 5);
int subtraction = Calculator::Subtract(10, 5);
int multiplication = Calculator::Multiply(10, 5);
int division = Calculator::Divide(10, 5);
std::cout << "Sum: " << sum << std::endl;
std::cout << "Subtraction: " << subtraction << std::endl;
std::cout << "Multiplication: " << multiplication << std::endl;
std::cout << "Division: " << division << std::endl;
std::cout << "Total operations: " << Calculator::TotalOperations << std::endl;
return 0;
}
In this example:
- TotalOperations is a static field that counts the total number of operations performed by the
Add
,Subtract
,Multiply
, andDivide
methods. - The
Add
,Subtract
,Multiply
, andDivide
methods are static and update theTotalOperations
field each time they are called.