Static Members
👋 Hello everyone! Are you tired of getting confused about the difference between static and non-static
members in C++?
Look no further! In this post, we'll break down the concept of static members and how they differ from
non-static
members. We'll explore how memory addresses are related to these members and explain why static
attributes need to be
defined in a different way than non-static attributes. We'll also give some exceptions to the rules when
it comes to
defining static attributes.
A member can either be an attribute or a method. However, the behavior of non-static and static members
differs greatly
when it comes to memory allocation.
📍Member’s memory allocation
Non-static attributes are unique to each object and stored in a separate memory address. In contrast,
static attributes
are considered class attributes and their values are shared across all objects of the same class, which
means that
modifying a static attribute affects every single object of the class.
Nevertheless, it’s a bit different when talking about methods. Non-static and static methods are shared
across all
objects of a class, and all objects have access to use the same memory address for both types of
methods.
✅ Defining static attributes:
As long as we think deeply about it, an attribute of an object is defined in a constructor method once
the object to
which it belongs, is defined, for instance, in the main function, nevertheless, if a static attribute is
not an object
attribute but a class attribute, what could happen whether we initialize it each time we initialize an
object? Well,
that’s the point. We need to initialize static attributes in a different way. From my perspective, a
static attribute
needs to be ready and defined before some object is being initialized by the user of our class, which
means, we need to
initialize our static attribute, for instance, in the global scope of our .cpp, being it useful for
applying data
hiring, one of the most important characteristics and principles of object oriented programming. Why is
that? Because we
are defining our static attribute within the .cpp file, and a supposed user of our class won’t read our
.cpp but the
.hpp due to the fact that they need to know the declaration of our class in order to manage to use it.
✅ Defining static method:
When it comes to static methods, it is done in the same way as a non-static method. However, it is
important to note
that a static method does not have a 'this' pointer as it is considered a class method.
⚠️ Exceptions:
Finally, there are some exceptions to defining static attributes within a class, which are only possible
when the
attribute is constant and of specific data types such as bool, char, int, longint, unsigned, etc.
Here some examples:
static const char myChar = “c”;
static const int myInt = 100;
I hope this explanation helps you gain a better understanding of static members in C++. If you have any
questions or
comments, please feel free to leave them below! 😊