In a previous post I wrote about Python’s elegance and some tips to make your code look even fancier and closer to the English language. I’m currently rather busy programming in C++, a language I have not used for 2 years, and the code certainly looks dirtier. ­čÖü

Sometimes I want to make a class attribute private, so in order to access it one has to implement a read method and (if needed) a write one. They’re called getter and setter respectively (I’m not very fond of private attributes, by the way). This usually happens when the external representation of the attribute value differs with the internal one.

Both Microsoft Visual C++ and Borland Builder C++ implement getters and setters in their own way, but I prefer a standard method which can be used across every C++ standard compiler (including Intel C++ and G++, the GNU C++ compiler). Finally I worked out a solution and implemented this template, and it seems to work. Basically, it implements properties as classes:

You will usually put this template into an include file.

Now an example. It creates an <int> container (stored in an STL <vector>), with a length property. Reading this property returns vector.size() value, whilst setting a value to length will effectively resize the vector:


effectively sets the vector length and

read such length.

Note: This code is released with a CC0 (Public Domain) license, so you can use it for whatever you want.

Read more:


Comments are closed.