Da Xiong's static study notes

Written in front,
This is the note I wrote after reading other people's articles, which is almost the same as the original text. In the future, I will also add the relevant things I learned in other places, that is to say, this is just my own note.
Original link: https://github.com/Light-City/CPlusPlusThings/tree/master/basic_content/static

When used with different types, the static keyword has different meanings. We can use the static keyword:
1. Static variables: variables in functions and variables in classes
2. Members of static classes: class objects and functions in classes
*
*

1. Static variables in functions

When a variable is declared static, space is allocated over the life of the program. Even if the function is called many times, the space of static variables is allocated only once, and the variable value in the previous call is passed through the next function call. This is useful in C / C + + or any other application that needs to store the state of previous functions.

#include <iostream> 
#include <string> 
using namespace std; 

void demo() 
{ 
	// static variable 
	static int count = 0; 
	cout << count << " "; 
	
	// value is updated and will be carried to next function calls 
	count++; 
} 

int main() 
{ 
	for (int i=0; i<5; i++)	 
		demo(); 
	return 0; 
} 

Output:

0 1 2 3 4

You can see in the above program that the variable count is declared as static. Therefore, its value is passed through function calls. The variable count is not initialized each time the function is called

2. Static variables in class

Since variables declared as static are initialized only once, because they allocate space in separate static storage, static variables in the class are shared by objects. You cannot have multiple copies of the same static variable for different objects. For this reason, static variables cannot be initialized with constructors.

#include<iostream> 
using namespace std; 

class Apple 
{ 
public: 
	static int i; 
	
	Apple() 
	{ 
		// Do nothing 
	}; 
}; 

int main() 
{ 
Apple obj1; 
Apple obj2; 
obj1.i =2; 
obj2.i = 3; 
	
// prints value of i 
cout << obj1.i<<" "<<obj2.i; 
} 

As you can see in the above program, we have tried to create multiple copies of the static variable i for multiple objects. But that didn't happen. Therefore, the static variables in the class should be explicitly initialized by the user using the class name and range resolution operator outside the class, as shown below:

#include<iostream> 
using namespace std; 

class Apple 
{ 
public: 
	static int i; 
	
	Apple() 
	{ 
		// Do nothing 
	}; 
}; 

int Apple::i = 1; 

int main() 
{ 
	Apple obj; 
	// prints value of i 
	cout << obj.i; 
} 

Output:

1

Summary:

  • The static member does not belong to any object of the class, so it is not defined when the object is created, so it cannot be initialized by the constructor of the class, and generally cannot be initialized in the class.
  • The keyword static only appears inside the class.
  • Defined and initialized outside the class
  • Constant static members can be initialized within a class

3. Static objects in class

Like variables, objects have a range when declared static until the life cycle of the program. Consider the following procedure, where the object is non static.

#include<iostream> 
using namespace std; 

class Apple 
{ 
	int i; 
	public: 
		Apple() 
		{ 
			i = 0; 
			cout << "Inside Constructor\n"; 
		} 
		~Apple() 
		{ 
			cout << "Inside Destructor\n"; 
		} 
}; 

int main() 
{ 
	int x = 0; 
	if (x==0) 
	{ 
		Apple obj; 
	} 
	cout << "End of main\n"; 
} 

Output:

Inside Constructor
Inside Destructor
End of main

In the above program, the object is declared non static within the if block. Therefore, the scope of the variable is only within the if block. Therefore, when an object is created, the constructor will be called and the destructor will be called at the same time when the control of the if block is crossed, because the scope of the object is only within the if block that declares it. If we declare the object static, now let's look at the changes in the output.

#include<iostream> 
using namespace std; 

class Apple 
{ 
	int i; 
	public: 
		Apple() 
		{ 
			i = 0; 
			cout << "Inside Constructor\n"; 
		} 
		~Apple() 
		{ 
			cout << "Inside Destructor\n"; 
		} 
}; 

int main() 
{ 
	int x = 0; 
	if (x==0) 
	{ 
		static Apple obj; 
	} 
	cout << "End of main\n"; 
} 

Output:

Inside Constructor
End of main
Inside Destructor

You can clearly see the changes in the output. Now, call the destructor after main ends. This is because the scope of static objects runs through the life cycle of the program.

Static functions in classes

(1)
Just like static data members or static variables in a class, static member functions do not depend on the object of the class. We are allowed to use objects and '.' To call a static member function. However, it is recommended to call static members using class name and range resolution operators.
Static member functions are allowed to access only static data members or other static member functions, which cannot access non static data members or member functions of the class.

#include<iostream> 
using namespace std; 

class Apple 
{ 
    public: 
        // static member function 
        static void printMsg() 
        {
            cout<<"Welcome to Apple!"; 
        }
}; 

// main function 
int main() 
{ 
    // invoking a static member function 
    Apple::printMsg(); 
} 

Output:

Welcome to Apple!

(2)

  • Static member functions are special member functions in a class
  • Static member functions have no hidden this pointer: when an object's non static member function is called, the system will assign the starting address of the object to the this pointer of the member function. However, the static member function does not belong to an object and is shared by all objects of the class, so the static member function has no this pointer
  • Static member functions can be accessed directly through the class name
  • Static member functions can be accessed through objects
  • Static member functions can only directly access static member variables (functions), not ordinary member variables (functions)

Tags: C++

Posted by cscheuble on Sun, 22 May 2022 16:38:40 +0300