propertypp: Objective-C-like properties for C++ classes

The short version: propertypp on GitHub

The long version:

A co-worker recently told me he had made some member variables in a C++ class public, because a getter and setter for them would just do exactly the same thing as reading and writing those member variables directly. I told him that was bad design, and he told me he was aware of it but that in that particular case making those member variables public would hardly have any negative impact on the system, because it was a small piece of code with a very limited and controlled scope. I told him he should still consider making those member variables private and writing getters and setters, but then he admitted he found the get*() and set*() syntax too cumbersome.

I agree with him on that last point. Calling getters and setters makes code so ugly when you could be using dot notation. Objective-C and other languages have this nice little feature called properties, which allow you to use dot notation for accessing member variables of a class, but having getters and setters getting called behind the scenes.

I began wondering if it would be possible to implement properties in C++ and soon realized it is, indeed, possible. You can declare public member variables of wrapper classes that overload cast and assignment operators and read and write private member variables containing property values. If that explanation wasn’t enough to help you picture the solution, here are some example implementations:

Those aren’t the only C++ property implementations available out there, but I found that all implementations required too much code to be used.

I set out to write a generic C++ property library that required as little effort to be used by the programmer as possible. propertypp is the result of that. I wont’t go into detail here on how to use it and what code that uses it looks like, because it’s all there on the README on GitHub.

Notice how I’ve managed to have the name of the class containing the properties to be informed only on getter and setter synthetization. If you read the code on propertypp.h, you’ll see there was no way out of that. Another nice thing is that it doesn’t have any dependencies on third-party libraries: it’s just plain C++ and STL. My initial, unpublished implementation used Boost.Function to wrap the function objects for getters and setters, but I didn’t want to impose that dependency on every project that shall use propertypp.

propertypp is far from finished. At present, it only works with fundamental data types and pointers. That prevents having a struct or object as a property, unless it’s a pointer to that struct or object. I also want to support read-only properties.

I have to admit I’m quite proud of my work on propertypp. So far, the people I’ve shown it to thought it nice. However, criticism will be appreciated if anyone has any 🙂

1 comment so far ↓

#1 Fabio Utzig on 09.18.11 at 11:53 PM

Hey, nice article! I was reading your code and it really makes me cry. But it’s not your fault! Your code is actually very good. I guess you’ve been reading Sutter/Alexandrescu ’cause some parts of the code are so complicated that it has probably irreversibly destroyed some of my neurons.

But the real problem here is C++. C++ can be really ugly sometimes. The idea of writing an Objective-C property’s like feature for C++ is not bad. But I think the whole idea of properties in Obj-C is a fail. This @property/@synthesize thing is just messy IMO. And in propertypp, for example, just having to overload operators for +=, -=, *=, etc, is really trying hard to fix something which is a “fail” in the language. I usually write my code with inline set/get in a Qtish dialect like this:

[sourcecode language=”c++”]
class Klass
int x() const { return x_; }
void setX(const int x) { x_ = x; }

int x_;

It aesthetically pleasing for my eyes and very simple. I guess the number of lines is not very different from the version using propertypp huh? 😛

But if you want to see something really nice, just check out this Ruby snippet:

[sourcecode language=”ruby”]
class Klass
attr_accessor 😡
attr_reader :y

k =
k.x = 1
k.y = 2 // NoMethodError exception here!
puts k.x // prints 1
k.x += 2
puts k.x // prints 3

This is just plain beauty don’t you think? 🙂

By the way, what C++ books have you read?

Is there a way to enable a “preview” feature when posting comments?

Leave a Comment