In C and C++ we use the preprocessor macro NULL to say a pointer is not pointing to anywhere right now. The problem with NULL is that underneath it is just a plain 0. Consider the problem looking at this code excerpt: If you try to compile it using g++ 4.6, you will get something… Continue reading C++11: nullptr
In C++03, if I have a vector and want to show its elements, I could do something like this: That method will be useful to show any collection that has a begin() and an end() and an iterator with an operator++(int), an operator!=() and an operator*() properly implemented. C++0x provides us a very useful range-based… Continue reading C++11: Range-based for loop
Consider this example: You have a class A and a container called List<T>. As shown in the code below, the container is just a wrapper of a std::vector. You also have a function called getNObjects that returns a list containing N instances of the class A. If you run this code, you will see an… Continue reading C++11: Move semantics
Consider you have a Person class: You want to store several instances of this class in a vector: If you want to sort this vector by the person ID, you could implement a PersonComparator and use the STL sort algorithm: In C++03 you need to create a separate class (or alternatively a function) to use… Continue reading C++11: Lambda expressions
If you want to declare and initialize an array of five integers, you could do something like: But in C++03, what would happen if you would want to do the same thing using some STL container, like a std::vector? Something like: You guessed it!!! It would not get compiled. Initializer lists were just available for… Continue reading C++11: Initializer lists
decltype is a new keyword that infers the type of a given expression. Though it can be used instead of auto; it is used mainly on return types. For example; this function: could be written using decltype: The compiler evaluates 'a+b' and infers the type of such expression is 'int' and defines 'ret' as an… Continue reading C++11: ‘decltype’
The new standard of C++ is being released and it exposes a lot of features. Starting this post, I will write about such new features, how they are used and their benefits. Some bits of history: The last published C++ standard is called C++03 and was published, oh surprise!, in 2003. Since such day, a… Continue reading C++: ‘auto’
I was playing around with IKVM.NET (www.ikvm.net) and I found it very very interesting. IKVM.NET is a Java Virtual Machine implemented on top of .NET; and though this is already kind of weird, the most interesting thing is that inside IKVM, the Java objects of your Java application are first class objects of the CLR.… Continue reading Java: Writing a Windows Forms application using Java
I am just speculating here and the title of my post is just based on some tests I performed in my spare time; but, according to them, based on dynamic array lists and strings, Java is far faster than C++. The simple Java program is this: As you can see, the test is quite simple,… Continue reading C++: Is Java faster than C++?
The function implemented below, can be used to replace something like: to And performs far better. Obviously, my function simply performs its task and does not need to do all parsing and extra tasks that the printf family does.
Let's say we defined this class inside a Reader.DLL DLL: This class lets us read from a file once. What if later we want to use this same class to read from a file several times? We can modify it to: What I have done is adding the file name as a class attribute to… Continue reading C++: Pimpl
Let's consider the following class: This class will be useful as the base class for implementing several specialized writers (a file writer and a console writer come to my mind right now), for instance: Nice, isn't it? We can use the Writer::write() method to write on any kind of media depending on the subclass we… Continue reading C++: The curiously recurring template pattern
When you implement something like: You are saying the C++ compiler: "Please, use the first template for all the MyClass<T> instances with any T except for the MyClass<T> instances where T = char". The problem I see with this approach is that you must provide full implementations for the template and for the <char> specialization… Continue reading C++: Inheritance on template specialization
Hello, in this blog I want to publish some interesting information about C++ and other related topics (object oriented programming, generic programming, etc.); the idea is having this information useful and very accurate; so, since I am a fan of these topics but I am far from being an expert on them, I want you… Continue reading C++: First message