Ubiquitous use of smart pointers can prevent memory leaks and make for much easier to read and understand code. Unfortunately, as with most things C++, there are some caveats you need to be aware of otherwise your attempts to write robust code could very well come back to bite you. This little quiz shows how […]
When working with STL containers we generally use iterators to access, manipulate and enumerate the contents. This almost becomes second nature and it’s very easy to go on auto-pilot and end up coding an innocuous looking bit of code that can contain a rather nasty surprise. This little quiz shows just one example of how […]
One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property. These are like data members except they can have preconditions imposed on them prior to getting or setting their value.
The C++ language is a context sensitive language, which means a compiler cannot always decide the semantics of a line of code in isolation. Sometimes, though, it is impossible for the compiler to make up it’s mind so it just guesses. Yup, that’s right, it guesses. To find out more try this little quiz.
In general, it’s pretty obvious what order the compiler will initialize variables: it’s the order in which they appear in the translation unit. What happens, though, when you have a global variable in one translation unit depending on the the initialization of a global variable in another translation unit? This little quiz explores just that.
What could be as simple as incrementing a variable by one? Ignoring overflow, what else could possibly go wrong? As it turns out, quite a lot as this little quiz demonstrates.
There are various dangers when casting pointers to different types but as a general rule, casting to a void pointer and back to the original pointer is considered safe. Unfortunately, this is not always the case as this little quiz demonstrates.
Do you know how to access c-style literal strings? Try this two-part quiz and see if you are able to unravel the different semantics of character pointers and arrays.
The C++ Standard is a pretty large and complex document; however, it is the bible as far as writing C++ code is concerned. The standard is full of exceptions that prove the rule, and this quiz demonstrates just one trivial example.
This little quiz explores the pitfalls of trying to compare structs. Do you know the right way to check if two structs are the same?
The STL (Standard Template Library) is a collection of generic algorithms and data structures. This little quiz demonstrates one of the many useful things one can achieve with just a few lines of code when utilizing the power of this library.
The following question was the inspiration for this short article:”Splitting a template and class into definition and declaration.“. In this question the asker asks, “I have the code below, which is all well and good but I’d like to move the definition of the setListener method to the cpp file, yet I seem to be […]
Often, when implementing a feature, you won’t know how certain events should be handled at the point where they occur and you’d rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, what should it do now that it has this tag?
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. Advances in compiler optimizations have all but eliminated this concern thanks to a clever set […]
Unlike C#, C++ doesn’t have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism.
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their way to become runtime problems are harder to detect […]
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generate a log or trace file for debugging purposes. The relationship of the types may […]
Necessity is the mother of invention One thing that always amazes me is how often I see C++ programmers reinvent the wheel. The times I’ve seen engineers writing yet another sort routine or implementing their own version of a linked list, because they have a “special case” that necessitates it, just beggars belief. A “special […]