We fulfill power fantasies

RSS Feed Back

C++ gripes #1: static functions as friends

20.1.2018 19:01:56

While programming as a an activity is something I, as many others, derive joy out of, there are obstacles that make it less appealing from time to time. One of those obstacles is C++. I know picking on C++ is a bit of a fashionable thing to do at the moment, but it's no wonder why: the language is easy to pick on (and it's widely used, for what ever historical reason). Well, lately I've been involved in a project utilising sepples and it's features heavily, and here's my latest gripe with the language: headers polluted with implementation details.

In C, if you want to write a static function inside your source file, unknown to the header file, and the function manipulates some data structure defined in the header, you can. For example:

foo.h:
----------------------------
struct my_struct {int x;};
----------------------------

foo.c:
----------------------------
static void
_modify_struct(struct my_struct *s)
    {s->x = 5;}
----------------------------
Well, in the world of sepples where private and public are a (rather useless) concept, it's not that simple to keep our _modify_struct() function out of the user's sight. At least for now, I could not find a way to write the equivalent of the C example up there for a C++ class in the case where the function is to modify the class' private members, not without declaring the function in the header first.

So yeah, I'm not a fan of sepples.