Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C++
Article

C++ object properties with no run-time or memory overhead

Rate me:
Please Sign up or sign in to vote.
3.00/5 (16 votes)
19 Dec 20051 min read 94K   15   34
An article on how to make C++ objects with properties that have no run-time or memory overhead.

Introduction

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 code

The following two macros do the job:

#include "stddef.h"

#define CLASS(NAME)\
    typedef NAME ClassType;

#define PROPERTY(TYPE, NAME)\
    private:\
    class NAME##_class\
    {\
    public:\
        operator TYPE () const\
        {\
            return get_owner_object_const()->get_##NAME();\
        }\
        TYPE operator = (TYPE val)\
        {\
            get_owner_object()->set_##NAME(val);\
            return val;\
        }\
    private:\
        ClassType *get_owner_object() const\
        {\
            ClassType *owner_object = 
              (ClassType *)((char *)this - offsetof(ClassType, NAME));\
            return owner_object;\
        }\
        const ClassType *get_owner_object_const() const\
        {\
            return get_owner_object();\
        }\
    };\
    friend class NAME##_class;\
    public:\
    NAME##_class NAME;

Using properties

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 get_ and set_, suffixed with the property's name.

Example

#include "iostream"
using namespace std;

class Foo
{
public:
    Foo() : m_data(0)
    {
    }

    CLASS(Foo);
    PROPERTY(int, data);

private:
    int m_data;

    int get_data() const
    {
        return m_data;
    }

    void set_data(int val)
    {
        m_data = val;
    }
};

int main(int argc, char* argv[])
{
    Foo foo1;

    foo1.data = 5;
    int i = foo1.data;
    cout << i;
    return 0;
}

How it works

The macro 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 offsetof macro.

Notes

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Greece Greece
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalit's very good [modified] Pin
ztf762222-Aug-06 2:32
ztf762222-Aug-06 2:32 
GeneralI like it Pin
Steven Burns5-Mar-06 17:35
Steven Burns5-Mar-06 17:35 
GeneralSeriously Pin
armentage20-Dec-05 4:03
armentage20-Dec-05 4:03 
GeneralRe: Seriously Pin
Achilleas Margaritis21-Dec-05 1:57
Achilleas Margaritis21-Dec-05 1:57 
GeneralRe: Seriously Pin
armentage21-Dec-05 3:30
armentage21-Dec-05 3:30 
GeneralRe: Seriously Pin
SSacek27-Dec-05 10:58
SSacek27-Dec-05 10:58 
GeneralRe: Seriously Pin
armentage27-Dec-05 11:08
armentage27-Dec-05 11:08 
GeneralRe: Seriously Pin
SSacek27-Dec-05 11:25
SSacek27-Dec-05 11:25 
GeneralRe: Seriously Pin
Zac Howland28-Dec-05 18:56
Zac Howland28-Dec-05 18:56 
GeneralMacros are bad Pin
[Deep]Xor20-Dec-05 2:21
[Deep]Xor20-Dec-05 2:21 
GeneralRe: Macros are bad Pin
Achilleas Margaritis21-Dec-05 1:59
Achilleas Margaritis21-Dec-05 1:59 
GeneralRe: Macros are bad Pin
[Deep]Xor21-Dec-05 2:26
[Deep]Xor21-Dec-05 2:26 
GeneralRe: Macros are bad Pin
[Deep]Xor21-Dec-05 2:45
[Deep]Xor21-Dec-05 2:45 
GeneralRe: Macros are bad Pin
Oleksiy Vasylyev10-Jan-06 20:49
Oleksiy Vasylyev10-Jan-06 20:49 
GeneralRe: Macros are bad Pin
[Deep]Xor10-Jan-06 20:55
[Deep]Xor10-Jan-06 20:55 
GeneralRe: Macros are bad Pin
Andrey Belykh24-Apr-21 14:13
Andrey Belykh24-Apr-21 14:13 
GeneralSeems very familiar... Pin
Jason.King.Work@gmail.com19-Dec-05 8:34
Jason.King.Work@gmail.com19-Dec-05 8:34 
GeneralRe: Seems very familiar... Pin
Achilleas Margaritis21-Dec-05 2:01
Achilleas Margaritis21-Dec-05 2:01 
GeneralRe: Seems very familiar... Pin
Jason.King.Work@gmail.com21-Dec-05 7:34
Jason.King.Work@gmail.com21-Dec-05 7:34 
GeneralRe: Seems very familiar... Pin
Zac Howland28-Dec-05 19:02
Zac Howland28-Dec-05 19:02 
General4 bytes per property is not &quot;no memory overhead&quot; Pin
Hal Angseesing19-Dec-05 5:42
professionalHal Angseesing19-Dec-05 5:42 
GeneralRe: 4 bytes per property is not &amp;quot;no memory overhead&amp;quot; Pin
Achilleas Margaritis21-Dec-05 2:09
Achilleas Margaritis21-Dec-05 2:09 
QuestionDo you remember ... Pin
SilentSilent19-Dec-05 5:20
SilentSilent19-Dec-05 5:20 
GeneralC++ is C++ Pin
toxcct19-Dec-05 4:04
toxcct19-Dec-05 4:04 
GeneralRe: C++ is C++ Pin
Nemanja Trifunovic19-Dec-05 4:29
Nemanja Trifunovic19-Dec-05 4:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.