Skip to main content

Going up step by step



Have you ever wanted to be able to iterate through your own class as if it was a cool STL container?
Do something like this:

Then it there is a way to integrate your class into STL and get powerful support. In order to be able to provide interface to for each loop you need to support in your custom classes set of operations such as:

After giving it a good time to fix all the bugs and understand them all as single piece you will notice what really for each loop needs to be able to iterate through a custom object.

Here a code example. Simple one.

If you have a big class on your hands that stores all the data and you want to implement a more secure and encapsulated class you can implement a set of Wrappers and Iterators to iterate all of those edges of your class.

This code sample is a bit crazy. But it shows how you can separate iteration routine and the rest of class logic. Controlling complexity is a goal of software architecture. Patterns, SOLID and other instruments are there only cause of some bright minds wanted to show their jobs on that topic for other people to use it.

Iterators are neat concept separating STL algorithms and containers. Watching on Iterator pattern from another angle reveals different way to look at it's place in STL. Since Iterators usually implemented inside of containers they as glue connects two islands of the STL archipelago.

It's better to think about this relations as if there is a strictly established policy of data translation. Such high level of abstraction model let us see what the idea behind design of STL, as well as start to see other libraries on such level.

Iterate and prosper.

Popular posts from this blog

Constness of an object

Defining a function you might want to make some of it's arguments to be constant to avoid unnecessary copying or have a bit more strict interface. There is another way to use constness - there we meet constant class members.

Extend your list of debug tools with Sanitizers

How many ways to check if code is OK do you know? Probably the one would make a list with debuggers, logging, static/dynamic checkers, profilers and some other tools. The tools the one has in the list are there cause they've been checked with a time. They don't cover all the possible cases of mistakes that could be made during coding phase of a project, but they cover necessary minimum. Today we will try to extend our tool set with a young tools that already got into clang and if you have clang on your machine you can give it a try.