C++ does not have real object properties like C# or VB. It is possible to add properties to C++ objects by using the preprocessor that works in the exact same way as in C#, i.e., without execution and memory overhead.
Properties have the advantage of simplifying an object's interface. With properties, there is no need to remember the prefix of a 'getter' function: is it 'get', 'is' or 'has'? Properties behave like simple data members, but when they are assigned, they invoke the owner object code.
The following two macros do the job:
#include <span class="code-string">"stddef.h"
typedef NAME ClassType;
#define PROPERTY(TYPE, NAME)\
operator TYPE () const\
TYPE operator = (TYPE val)\
ClassType *get_owner_object() const\
ClassType *owner_object =
(ClassType *)((char *)this - offsetof(ClassType, NAME));\
const ClassType *get_owner_object_const() const\
friend class NAME##_class;\
Using properties is fairly straightforward:
- include the macro
CLASS, passing the class name, in the public portion of your class.
- declare your properties with the macro
PROPERTY(TYPE, NAME). The '
type' is the interface type, not the internal type of the property.
- place two private functions in your code that start with
set_, suffixed with the property's name.
#include <span class="code-string">"iostream"
using namespace std;
Foo() : m_data(0)
int get_data() const
void set_data(int val)
m_data = val;
int main(int argc, char* argv)
foo1.data = 5;
int i = foo1.data;
cout << i;
How it works
PROPERTY is a proxy class that diverts set/get requests to the owner object's set method. The address of the owner object is calculated by the
There is no run-time cost for this solution, except for four bytes added for each member: the C++ standard dictates that empty members should have a physical representation, in order to have pointers to them. A good compiler will eliminate the proxy calls and invoke the set and get of the owner object directly.
The code above could be extended with providing read only and write only properties, as well as indexed properties.
This article is provided as an example only. Your needs may vary.