Have you have thought to yourself that you wanted to provide a readable version of a variable in the public area of a class in C++, but without providing a getter function? Well, that’s not possible, since you an only grant full access or no access to a variable, but you can do a hack that makes it look like you haven’t provided a getter function — the key is to use operator overloading.
The concept is something similar to this:
class example {
private:
int c;
public:
const int c;
};
The idea is to make c fully accessible in the example class, but only readable in the public. There are a few options to doing this — the union way and the other way.
The union way has the nice advantage that it doesn’t use extra memory (and it can even be combined with the other ways) but it’s not always a good option because you cannot inherit from unions nor can unions inherit anything. Unions therefor are only usefull if you are writing a class that you know will never be derived.
The union way would be:
union example {
private:
int c_;
public:
class { operator int() { return ((example*)(this))->c_; } c;
};
Reading example::c will return the value of example::c_, but doesn’t allow example::c_ to be written to. The union way is simple and neat. It doesn’t mess up readability of the code a lot. The only reason why it’s not perfect is that you can’t inherit from a union or let classes/structs/unions inherit from unions. While I’m still at unions, you can imbed a struct in the union if you need more private variables, like so:
union example {
private:
struct { int c_; int b_; };
public:
class { operator int() { return ((example*)(this))->c_; } c;
class { operator int() { return ((example*)(this))->b_; } b;
};
This will do the same as above, only with two members.
A more extensible trick, which is also far less readable and a lot more convoluted, has to be used if you want something you can derive from. I haven’t fully explored the effects of this yet (such as what happens if you derive from it), but here goes:
class example
{
unsigned p_;
public:
class p_traits {
example * o;
public:
p_traits(example * e) : o(e) { };
operator unsigned() { return (o->p_); }
} p;
example(int i) : p_(i), p(this) { };
};
This is not nice code, and it can be made nice, but then it will be a lot more convoluted and less readable (but has more features).
The code is a single class which has a single unsigned private variable p_ and a public member p. p is, just like the example above, readable from outside the class, but is only writeable inside the class. I have not found a way to have several readable entries but without having full object overhead. The real problem however is that you must have a pointer to the owner of the class, which, to me, is a waste (this is why I prefer the union way in all the cases I can do it).
Both these techniques can be expanded to allow assigning values to the public portion or doing anything to them really, such as calling doing calculations or sending a message to a server. It could for example be used to change the size of a window in a graphical environment in a more “natural” way than the regular window.resize(100,100) — instead you could do window.width = 100, window.height=100. This is, of course just bells and whistles and doesn’t make the language more powerful, but I do like the idea of setting values, rather than calling functions (even if there’s no real difference) — it feels more object oriented this way.
Oh, and I don’t use the above non-union way exactly. Instead I have the p_traits class outside of the class which inherits from a common base class. The base class is a friend of the owner class and so the traits can access anything they want to.
55.396241
10.390635