Practical use case of Static | Sololearn: Learn to code for FREE!

+3

Practical use case of Static

Hi We do have static member variable and static member functions. I am aware that static member variables will not have multiple copies due to object and it will be only one class level allocation. Static member functions can only access the static members. Practical use of static function and variable is singleton design pattern as well as how many objects got created of some type. What else in terms of real example for static?

3/21/2022 1:03:43 PM

Ketan Lalcheta

9 Answers

New Answer

+2

Alot of use of static data member. Take a look in this code- https://code.sololearn.com/cIEqsY8auA9J/?ref=app Array in a class is static,Hence it's common for a whole class. Whenever i put pass student names via constructor from my main method,It gets store in array,I can access that array by creating a new class object and then by calling AvailableStudents function.

+2

https://code.sololearn.com/cQHLa1LlCSVx/?ref=app In this code,I ordered my prorgam to increment by 1 whenever the Fun(); function is called.(I called Fun() function 4 times,Hence 1,2,3,4). Just remove the static keyword and see what's going to be happen. No matter how many times you call Fun() function,It will show 1. To get more clearity on static keyword, Please watch code with harry's video - https://youtu.be/QcLI2zGVYFo (It's in Hindi BTW). You won't understand by typing as much as you would understand by this video about static keyword.

+1

coroutines

+1

Ketan Lalcheta Making array as a static in this case helps me to create an array constant for all object that are gonna created of Student class . Means if i pass. Student s("Mikasa"); By s object via constructor,So i could store Mikasa in array. If i invoke AvailableStudents function by another object, Like Student s2; s2.AvailableStudents(); So,This function basically prints the available students, And Mikasa also included,Even though i stored her in array by different object( s object) and can access her by s2 object because array is constant for all the object's that are going to create of Student class. Static data member is very useful in many cases too.

+1

Ketan Lalcheta In this article youll find and explanation and implementation of coroutines https://www.geeksforgeeks.org/coroutines-in-c-cpp/

0

Raul Ramirez thanks but I am not much aware about coroutines much and hence will explore and then try to corelate

0

Manav Roy[LESS ACTIVE DUE TO EXAMS] thanks.... But isn't it a good idea to use some other data structure to store objects of class student... This class can have only name as member variable and no need of static array...

0

Thanks but I would like to disagree on this use case. I am not saying that it's not correct or it's not good to do so... My main thing is that I am not comfortable with this use case of static . I might be wrong here also. Any other use case if you would like to share ? And once again thanks for your time, effort and help

0

Thanks Guys. One more use case I come across is to encapsulate data for a specific translation unit in c