C++ Are Static Methods Faster? (Pt. II)

As mentioned in my previous blog post, I hope to examine a more interesting comparison for the different types of methods. Note that this comparison completely ignores the different use-cases for each type, it is therefor only relevant for ‘pure functions’, as in that case it doesn’t matter whether they belong to a class or whether they are static or not.

Continue reading

C++ Are Static Methods Faster?

In comparison to Java, C++ does not force you to make all methods part of a class. Instead you can also have regular methods, which are often used for helper methods that only rely on their input parameters to generate an output. It is obvious that these methods don’t have to be part of a class as they do not require any state, they are what we call pure methods.

But you can also have regular methods and static methods outside of a class. Although this seems odd since isn’t static only saying that it does not require a class to be called, but since the method isn’t part of a class in the first place, what is the point of doing this?

Continue reading

C++ Extending Classes

In the previous blog post I wrote how you can have a simple ‘to string‘ method for a C++ object. My proposed solutions however, like in all languages, require you to have access to the code of that class and be able to alter it. There is however a way that allows you to ‘extend’ a class with some methods without needing actual access to the source code. In this blog post I will show you how you can implement output stream operator <<, and then discuss this approach more generally.

Continue reading

C++ Static Variables

The concept of a static variable exists in every language, and it is a feature that enables multiple objects to share a common variable. It enables communication between these multiple objects and can be used to avoid creating copies of certain data if this data is constant. There is however a big difference between Java and C++, whereas in Java you can only have static members, in C++ you can have static on all levels going from global variables all the way down to (function) local ones.

In this blog post I will briefly discuss the sense and nonsense of static variables and how they perform. Based on that I will give my opinion of when and how they should be used.

Continue reading

C++ String Representation

It won’t be long before you run into the situation where you want to print an object. And when you do so, you of course want to have a meaningful representation of your object and not just the memory address. In Java and Python you have ‘special’ methods that are meant to do this translation for you, being toString and __str__ and __repr__ respectively. But what about C++?

Continue reading

C++ Inline Performance

In the previous post we established there was little to be gained to manually mark a function as inline, however all of this was done with functions that were declared in a header file as this is a requirement for inlining. In this blog post we will see how an inlined function (declared in the header) compares to a function declared in a source file.

Continue reading

C++ Automatic inlining?

C++ has the option to mark a function for ‘inlining’, this means that the function is ‘copied’ to where it is called and thus removing the overhead of calling the function. I believe to have read somewhere that the compiler can be smart enough to perform this optimization for you and that you shouldn’t care about explicitly marking such a method. It is time to put this hypothesis to the test.

Continue reading