C++ - Notes

Static Function/Variables in a class

Long time ago, I found a really nice example to understand it and I’m writing this post to document it. It took me a long time to understand what a static member/function is, why and when I should use it.

Creating a toy class

Let’s create small code where we will emulate a service with users. We want to know at any time how many users we have, how can we do that?

Code for the user class:

class User{

	public:
		std::string name;
		User(){ // Constr

		};
		~User(){  //Destr

		};
};

Counting the users in the main function:

int main(){
	int user_counter = 2 ;
	User user1, user2;

	user2.~User();
	user_counter--;
}

This is not a good solution, I would have to count every time I create/delete a new user by hand. Wouldn’t it be nice to make c++ count for us? This is why we want to use a single variable inside the class to count: This is a static variable.

Creating the user class with a static counter

class User{

	static int user_counter;
	public:
		std::string name;

		User(){ // Constr
			user_counter++;
		};

		~User(){  //Destr
			user_counter--;
		};
};

Now, the user_counter value goes up/down when the constructor/destructor is called.

int main(){
	User user1, user2; //user_counter = 2 
	user2.~User(); //user_counter = 1
}

How do we access this static counter?: Static Functions

“Normal” functions can’t access static variables, and it makes sense since these variables are global to every instance of the class, i.e. the number of users can’t be accessed easily as it’s shared by everyone.

class User{

	static int user_counter;
	public:
		std::string name;

		static int get_user_count() {return user_counter;}

		User(){ // Constr
			user_counter++;
		};

		~User(){  //Destr
			user_counter--;
		};
};

Great, but how do i initiate the user_counter variable? It’s a shared variable, so we can’t simply write static int user_counter = 0 inside the user class, so need to initiate outside of it!

//Below the User class
int User::user_counter = 0; 

int main(){
	std::cout << User::get_user_count() << "\n";
	User user1, user2; //user_counter = 2

	std::cout << User::get_user_count() << "\n";
	user2.~User(); //user_counter = 1

	std::cout << User::get_user_count() << "\n";	
}
Output:
0
2
1

The full working code:

#include <iostream>

class User{

	static int user_counter;
	public:
		std::string name;

		static int get_user_count() {return user_counter;}

		User(){ // Constr
			user_counter++;
		};

		~User(){  //Destr
			user_counter--;
		};
};

int User::user_counter = 0; 

int main(){
	std::cout << User::get_user_count() << "\n";
	User user1, user2; //user_counter = 2

	std::cout << User::get_user_count() << "\n";
	user2.~User(); //user_counter = 1

	std::cout << User::get_user_count() << "\n";	
}

istringstream from sstream:

This helps to get word from a string separated by a space.

...
std::string s = "foo bar";
std::istringstream p_s(s);

while(p_s) {
	p_s >> word; # get the word to the variable
	std::cout<<word<<"\n";
}
...
## Output ##
foo
bar

‘for_each’ superpower

Cpp reference

Lambda Expressions

Cpp reference

unordered_set vs unordered_map

  • Set doesn’t need a value associated with the key, it’s a hash table with keys only
  • map is a hashtable, key and value pair.

priority_queue

Import using #include <queue> By default:

* template<
    class T,
    class Container = std::vector<T>,
    class Compare = std::less<typename Container::value_type>
> class priority_queue;
//-- 
In: 9 8 7 6 5 4 3 2 1 0 
// using push!
Out: 0 1 2 3 4 5 6 7 8 9 
//--
>
 Date: May 17, 2022
 Tags:  coding c++ cpp

Previous
Kadane's Algorithm & LeetCode 53 Maximum Subarray

Next
Google SRE Book - Postmortem Culture [Extract]