When to use extern in C++

This comes in useful when you have global variables. You declare the existence of global variables in a header, so that each source file that includes the header knows about it, but you only need to “define” it once in one of your source files. To clarify, using extern int x; tells the compiler that … Read more

Stack Memory vs Heap Memory

I am programming in C++ and I am always wondering what exactly is stack memory vs heap memory. All I know is when I call new, I would get memory from heap. If if create local variables, I would get memory from stack. After some research on internet, the most common answer is stack memory … Read more

Incomplete Type Is Not Allowed

You are trying: but Node is not a complete type at this point (you are in fact defining it at this point), a structure cannot contain an instance of itself, it can contain a pointer or reference to an instance of itself but not an actual instance. After all, if such recursive containment were allowed, … Read more

Convert an int to ASCII character

Straightforward way: Safer way: Generic way: Handy way: C++ way: (taken from Dave18 answer) Boss way: Joe, write me an int to char converter Studboss way: char aChar = ‘6’; Joe’s way: char aChar = ‘6’; //int i = 6; Nasa’s way: //Waiting for reply from satellite… Alien’s way: ‘9’ //Greetings. God’s way: Bruh I built … Read more

C++ Singleton design pattern

In 2008 I provided a C++98 implementation of the Singleton design pattern that is lazy-evaluated, guaranteed-destruction, not-technically-thread-safe:Can any one provide me a sample of Singleton in c++? Here is an updated C++11 implementation of the Singleton design pattern that is lazy-evaluated, correctly-destroyed, and thread-safe. See this article about when to use a singleton: (not often)Singleton: … Read more

Read file line by line using ifstream in C++

First, make an ifstream: The two standard methods are: Assume that every line consists of two numbers and read token by token: int a, b; while (infile >> a >> b) { // process pair (a,b) } Line-based parsing, using string streams: #include <sstream> #include <string> std::string line; while (std::getline(infile, line)) { std::istringstream iss(line); int … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)