Better than Singletons: The Service Locator Pattern

One of the biggest problems in object-oriented programming is getting access to the objects you need.

A very common solution to this problem is dependency injection. This means you have to pass every method the objects it needs to do its work (you can also pass them to the constructor of the methods object and store them in member variables). What sounds easy and logical at first also gets messy quite quickly when applied in practice.

With dependency injection, you have to make sure the right objects are available where ever they are needed. That means you often have to pass several objects to the same method and you have to add new parameters to methods and constructors all the time. As your code grows bigger passing those objects through your project starts to feel very repetitive and when you refactor your code you have to touch a lot more methods than you would need to touch without dependency injection.

Continue reading “Better than Singletons: The Service Locator Pattern”

What is Name Mangling in C++?

When we write programs in C++, we create new functions and methods with (hopefully) descriptive names all the time. Names are important for us humans to understand code. But in the compiled machine code of our finished executables, they don’t play any role. The CPU doesn’t even know about them.

Whenever our program has loaded, all functions and methods are placed at specific memory addresses of the virtual address space assigned to the process of our running program. Whenever a function calls another function, it doesn’t call the name of the function but the address where the function is located in memory.

Continue reading “What is Name Mangling in C++?”