#include <iostream>
#include <string>
static int s_fileCounter = 100;
static void printFileMessage(const std::string& msg) {
std::cout << "[FileScope] " << msg << std::endl;
}
int incrementCallCount() {
static int callCount = 0;
callCount++;
std::cout << "incrementCallCount has been called " << callCount << " times" << std::endl;
return callCount;
}
class ObjectCounter {
private:
static int s_totalObjects;
int m_id;
public:
ObjectCounter() : m_id(s_totalObjects + 1) {
++s_totalObjects;
std::cout << "Created object #" << m_id << ", current total objects = " << s_totalObjects << std::endl;
}
~ObjectCounter() {
--s_totalObjects;
std::cout << "Destroyed object #" << m_id << ", remaining objects = " << s_totalObjects << std::endl;
}
static int getTotalObjects() {
return s_totalObjects;
}
static void showStatistics() {
std::cout << "[ObjectCounter] Current active object count: " << s_totalObjects << std::endl;
}
};
int ObjectCounter::s_totalObjects = 0;
class Singleton {
private:
Singleton() {
std::cout << "Singleton instance created" << std::endl;
}
~Singleton() = default;
public:
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
static Singleton& getInstance() {
static Singleton instance;
return instance;
}
void doSomething() const {
std::cout << "Singleton performing operation, address = " << this << std::endl;
}
};
class MathConstants {
public:
static const double PI;
static const int MAX_VALUE = 100;
static constexpr double E = 2.71828;
};
const double MathConstants::PI = 3.141592653589793;
int main() {
std::cout << "========== C++ static Keyword Demonstration ==========" << std::endl;
std::cout << "\n--- File scope static ---" << std::endl;
printFileMessage("Static function call");
std::cout << "Initial value of static global variable s_fileCounter = " << s_fileCounter << std::endl;
s_fileCounter += 50;
std::cout << "After modification s_fileCounter = " << s_fileCounter << std::endl;
std::cout << "\n--- Static local variables ---" << std::endl;
incrementCallCount();
incrementCallCount();
incrementCallCount();
std::cout << "\n--- Class static members ---" << std::endl;
ObjectCounter::showStatistics();
ObjectCounter obj1;
ObjectCounter obj2;
{
ObjectCounter obj3;
std::cout << "Inside scope, get total via static function: " << ObjectCounter::getTotalObjects() << std::endl;
}
ObjectCounter obj4;
ObjectCounter::showStatistics();
std::cout << "\n--- Singleton using static local variable ---" << std::endl;
Singleton& s1 = Singleton::getInstance();
Singleton& s2 = Singleton::getInstance();
s1.doSomething();
s2.doSomething();
if (&s1 == &s2) {
std::cout << "s1 and s2 are the same instance, same address: " << &s1 << std::endl;
}
std::cout << "\n--- Static constant members ---" << std::endl;
std::cout << "MathConstants::PI = " << MathConstants::PI << std::endl;
std::cout << "MathConstants::MAX_VALUE = " << MathConstants::MAX_VALUE << std::endl;
std::cout << "MathConstants::E = " << MathConstants::E << std::endl;
std::cout << "\n========== Program End ==========" << std::endl;
return 0;
}