C++ Field Accessors - A Cure for Public Field Phobia

03 Mar 2015

C++ Standard

Recently I was looking for some opinions regarding lack of field accessors in C++ standard. I came across a nice article A Modest Proposal for Curing the Public Field Phobia dated 2001. Since then C++ has matured a lot gaining amazing new features with C++11 and latest C++14 revision, but still some old annoyances like no field accessors remain.

The most often used argument against providing field accessors in C++, it that the language was designed to be verbose and explicit, so it is better to have obj.setX(1) than obj.x = 1, that may let you believe you are accessing some memory directly, while you are not. This leads use however to very obscure code like that:

node.setVisitCount(node.getVisitCount() + 1);

These who are not suffering public field phobia may define visitCount to be public field and use node.visitCount++ . But if it is a dynamic property then C++ does not give you many choices that to use former obscure syntax.

Read the rest of this entry ยป

Posted in C++, Programming