Difference between static and non-static members in C++

In C++, understanding the distinction between static and non-static members of a class is fundamental for effective object-oriented programming. Static members belong to the class itself rather than any specific class instance, allowing access without creating an instance. On the other hand, non-static members are tied to specific class instances.

Static members in C++

Static members of a class in C++ are shared across all instances of that class. They have a single copy in memory, regardless of how many objects of the class exist. Static members can include variables and methods, which are declared with the static keyword.

Advantages of static members

  • Shared Data: This is useful for class-wide constants or shared resources.

  • Utility Functions: This can be used without an object instance, ideal for utility functions that are relevant to the class but don’t operate on individual instances.

Declaring static members

Static variables and methods are declared within a class definition using the static keyword. However, static variables need to be defined outside the class as well.

Accessing static members

Static members are accessed using the class name and the scope resolution operator (::), rather than through an object of the class.

MyClass::staticMethod(); // Correct way to access a static method
MyClass::staticVar = 5; // Accessing static variable

Code example

Here is a coding example:

#include <iostream>
class MyClass {
public:
static int staticVar;
static void staticMethod() {
std::cout << "Accessing static method." << std::endl;
}
};
// Definition of static variable
int MyClass::staticVar = 0;
int main() {
// Accessing and modifying the static variable
std::cout << "Initial staticVar value: " << MyClass::staticVar << std::endl;
MyClass::staticVar = 5;
std::cout << "Modified staticVar value: " << MyClass::staticVar << std::endl;
// Accessing the static method
MyClass::staticMethod();
return 0;
}

Let’s see some explanation of the above code:

  • Lines 3–9: We define MyClass with a static member staticVar and a static method staticMethod().

  • Line 12: We initialize staticVar outside the class to demonstrate static variable initialization.

  • Lines 14–24: In the main function:

    • Print initial and modified staticVar values to show static variable access and modification.

    • Call staticMethod() to illustrate static method access without creating an instance.

Non-static members in C++

Non-static members are unique to each instance of a class. They represent the state of an object and consist of fields (variables) and methods that operate on the individual instances of the class they belong to.

Characteristics of non-static members

  • Instance specific: Each object maintains its copy.

  • Object state: This is used to represent the properties and behaviors of an object.

Declaring and accessing non-static members

Non-static members are declared without the static keyword and accessed through class objects.

Code example

Here is a coding example:

#include <iostream>
class MyClass {
public:
int nonStaticVar;
void nonStaticMethod() {
std::cout << "Accessing non-static method." << std::endl;
}
};
int main() {
MyClass obj; // Instantiating the class
obj.nonStaticVar = 10; // Accessing non-static variable
std::cout << "Value of nonStaticVar: " << obj.nonStaticVar << std::endl; // Displaying the value of nonStaticVar
obj.nonStaticMethod(); // Accessing non-static method
return 0;
}

Let’s see some explanation of the above code:

  • Lines 3–9: We define MyClass with a non-static member nonStaticVar and a non-static method nonStaticMethod().

  • Lines 11–19: In the main function:

    • Instantiate MyClass to demonstrate creating an object.

    • Access and modify nonStaticVar to illustrate working with non-static variables.

    • Call nonStaticMethod() to show how to access non-static methods through an object instance.

    • Output the value of nonStaticVar and a message from nonStaticMethod().

Key differences

Here are some key differences between static and non-static members:

Feature

Static Members

Non-Static Members

Memory Allocation

Single instance in memory for all objects

Separate copies in memory for each object

Access

Can be accessed without creating a class instance

Require an object to be accessed

Purpose

Suitable for shared data and utility functions.

Define the state and behavior of individual objects

Overriding

Cannot be overridden; static methods can be hidden in derived classes, but this is not the same as overriding

Support polymorphism and can be overridden in derived classes to provide specialized behavior.

Inheritance

Can be inherited but maintain the same implementation across all instances and derived classes unless hidden by redeclaration

Can be inherited and have different implementations in derived classes through overriding

Initialization

Can be initialized directly in their declaration or outside the class definition

Typically initialized through constructors or directly at the point of declaration within the class

Conclusion

The choice between static and non-static members in C++ depends on the specific requirements of the program. Static members are ideal for scenarios where a common state or behaviour is shared across all instances of a class, whereas non-static members are essential for defining the unique characteristics and actions of each object.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved