Click here to Skip to main content
13,086,120 members (84,084 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 19 Dec 2005

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

, 19 Dec 2005
Rate this:
Please Sign up or sign in to vote.
An article on how to make C++ objects with properties that have no run-time or memory overhead.


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 <span class="code-string">"stddef.h"</span>

#define CLASS(NAME)\
    typedef NAME ClassType;

    class NAME##_class\
        operator TYPE () const\
            return get_owner_object_const()->get_##NAME();\
        TYPE operator = (TYPE val)\
            return val;\
        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;\
    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.


#include <span class="code-string">"iostream"</span>
using namespace std;

class Foo
    Foo() : m_data(0)

    PROPERTY(int, data);

    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; = 5;
    int i =;
    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.


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.


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


About the Author

Achilleas Margaritis
Software Developer (Senior)
Greece Greece
No Biography provided

You may also be interested in...

Comments and Discussions

Generalit's very good [modified] Pin
ztf762222-Aug-06 2:32
memberztf762222-Aug-06 2:32 
GeneralI like it Pin
Steven Burns5-Mar-06 17:35
memberSteven Burns5-Mar-06 17:35 
GeneralSeriously Pin
armentage20-Dec-05 4:03
memberarmentage20-Dec-05 4:03 
GeneralRe: Seriously Pin
Achilleas Margaritis21-Dec-05 1:57
memberAchilleas Margaritis21-Dec-05 1:57 
GeneralRe: Seriously Pin
armentage21-Dec-05 3:30
memberarmentage21-Dec-05 3:30 
GeneralRe: Seriously Pin
SSacek27-Dec-05 10:58
memberSSacek27-Dec-05 10:58 
GeneralRe: Seriously Pin
armentage27-Dec-05 11:08
memberarmentage27-Dec-05 11:08 
GeneralRe: Seriously Pin
SSacek27-Dec-05 11:25
memberSSacek27-Dec-05 11:25 
GeneralRe: Seriously Pin
Zac Howland28-Dec-05 18:56
memberZac Howland28-Dec-05 18:56 
GeneralMacros are bad Pin
[Deep]Xor20-Dec-05 2:21
member[Deep]Xor20-Dec-05 2:21 
GeneralRe: Macros are bad Pin
Achilleas Margaritis21-Dec-05 1:59
memberAchilleas Margaritis21-Dec-05 1:59 
GeneralRe: Macros are bad Pin
[Deep]Xor21-Dec-05 2:26
member[Deep]Xor21-Dec-05 2:26 
GeneralRe: Macros are bad Pin
[Deep]Xor21-Dec-05 2:45
member[Deep]Xor21-Dec-05 2:45 
GeneralRe: Macros are bad Pin
Alexey Vasiliev10-Jan-06 20:49
memberAlexey Vasiliev10-Jan-06 20:49 
GeneralRe: Macros are bad Pin
[Deep]Xor10-Jan-06 20:55
member[Deep]Xor10-Jan-06 20:55 
GeneralSeems very familiar... Pin
Jason King19-Dec-05 8:34
memberJason King19-Dec-05 8:34 
GeneralRe: Seems very familiar... Pin
Achilleas Margaritis21-Dec-05 2:01
memberAchilleas Margaritis21-Dec-05 2:01 
GeneralRe: Seems very familiar... Pin
Jason King21-Dec-05 7:34
memberJason King21-Dec-05 7:34 
GeneralRe: Seems very familiar... Pin
Zac Howland28-Dec-05 19:02
memberZac Howland28-Dec-05 19:02 
General4 bytes per property is not &quot;no memory overhead&quot; Pin
Hal Angseesing19-Dec-05 5:42
memberHal 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
memberAchilleas Margaritis21-Dec-05 2:09 
QuestionDo you remember ... Pin
SilentSilent19-Dec-05 5:20
memberSilentSilent19-Dec-05 5:20 
GeneralC++ is C++ Pin
toxcct19-Dec-05 4:04
membertoxcct19-Dec-05 4:04 
GeneralRe: C++ is C++ Pin
Nemanja Trifunovic19-Dec-05 4:29
memberNemanja Trifunovic19-Dec-05 4:29 
GeneralRe: C++ is C++ Pin
toxcct19-Dec-05 4:34
membertoxcct19-Dec-05 4:34 
JokeRe: C++ is C++ Pin
spoulson219-Dec-05 4:46
memberspoulson219-Dec-05 4:46 
GeneralRe: C++ is C++ Pin
toxcct19-Dec-05 4:55
membertoxcct19-Dec-05 4:55 
GeneralRe: C++ is C++ Pin
Achilleas Margaritis19-Dec-05 5:13
memberAchilleas Margaritis19-Dec-05 5:13 
GeneralRe: C++ is C++ Pin
toxcct19-Dec-05 5:15
membertoxcct19-Dec-05 5:15 
GeneralRe: C++ is C++ Pin
Achilleas Margaritis21-Dec-05 2:10
memberAchilleas Margaritis21-Dec-05 2:10 
GeneralRe: C++ is C++ Pin
Nemanja Trifunovic19-Dec-05 5:22
memberNemanja Trifunovic19-Dec-05 5:22 
GeneralRe: C++ is C++ Pin
Rage20-Dec-05 4:17
memberRage20-Dec-05 4:17 
GeneralRe: C++ is C++ Pin
Achilleas Margaritis19-Dec-05 5:12
memberAchilleas Margaritis19-Dec-05 5:12 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170813.1 | Last Updated 19 Dec 2005
Article Copyright 2005 by Achilleas Margaritis
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid