Static Vs Const
Static Vs Const
examples
ChatGPT
In C++, both the static and const keywords are used to modify the behavior of
variables, but they serve different purposes.
static Keyword:
The static keyword is primarily used to control the lifetime and visibility of
variables within a specific scope.
When used with a global variable or a local variable within a function, it makes
the variable retain its value between function calls, effectively making it a
"static" variable.
When used with a class member variable (a static data member), it is shared among
all instances of the class, rather than being specific to each instance.
Example 1: Static local variable
cpp
Copy code
#include <iostream>
void demoStatic() {
static int count = 0; // Static local variable
count++;
std::cout << "Count: " << count << std::endl;
}
int main() {
demoStatic(); // Count: 1
demoStatic(); // Count: 2
demoStatic(); // Count: 3
return 0;
}
Example 2: Static data member in a class
cpp
Copy code
class MyClass {
public:
static int staticVar; // Static data member
};
int main() {
MyClass obj1, obj2;
obj1.staticVar = 5;
std::cout << "obj1.staticVar: " << obj1.staticVar << std::endl; //
obj1.staticVar: 5
std::cout << "obj2.staticVar: " << obj2.staticVar << std::endl; //
obj2.staticVar: 5
return 0;
}
const Keyword:
The const keyword is used to specify that a variable's value cannot be changed
after it is initialized. It ensures the variable is read-only.
It can be applied to local variables, function parameters, class member variables,
and pointers/references.
Example 3: Const local variable
cpp
Copy code
int main() {
const int x = 10; // Const local variable
// x = 20; // Error: Cannot modify a const variable
std::cout << "x: " << x << std::endl;
return 0;
}
Example 4: Const class member variable
cpp
Copy code
class MyClass {
public:
const int constVar; // Const data member
MyClass(int value) : constVar(value) {}
};
int main() {
MyClass obj(42);
// obj.constVar = 5; // Error: Cannot modify a const member variable
std::cout << "obj.constVar: " << obj.constVar << std::endl; // obj.constVar:
42
return 0;
}
In summary, static is used to control the lifetime and visibility of variables,
while const is used to make variables read-only and prevent their values from being
changed after initialization. These two keywords serve different purposes and can
also be used together in some contexts, such as defining a const static variable.
#include <iostream>
void demoStatic() {
static int count; // Static local variable
count =0;
count++;
std::cout << "Count: " << count << std::endl;
}
int main() {
demoStatic(); // Count: 1
demoStatic(); // Count: 1
demoStatic(); // Count: 1
return 0;
}
Static data members in C++ are used to store values common to the entire class. To
be more precise, it is like we learned about using global variables while using
functions. A static data member is available globally for all the objects of the
same class. Unlike the regular (non-static) data members of a class- A static data
member maintains its single copy that is shared by all the objects of the class. It
remains in the memory for the entire program but is visible only within the class
or the function within which it is defined.