Click here to Skip to main content
Click here to Skip to main content

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.

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

#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 <span class="code-string">"iostream"
</span>
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

Share

About the Author

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

Comments and Discussions

 
Generalit's very good [modified] Pinmemberztf762222-Aug-06 2:32 
GeneralI like it PinmemberSteven Burns5-Mar-06 17:35 
GeneralSeriously Pinmemberarmentage20-Dec-05 4:03 
GeneralRe: Seriously PinmemberAchilleas Margaritis21-Dec-05 1:57 
GeneralRe: Seriously Pinmemberarmentage21-Dec-05 3:30 
GeneralRe: Seriously PinmemberSSacek27-Dec-05 10:58 
GeneralRe: Seriously Pinmemberarmentage27-Dec-05 11:08 
GeneralRe: Seriously PinmemberSSacek27-Dec-05 11:25 
GeneralRe: Seriously PinmemberZac Howland28-Dec-05 18:56 
GeneralMacros are bad Pinmember[Deep]Xor20-Dec-05 2:21 
GeneralRe: Macros are bad PinmemberAchilleas Margaritis21-Dec-05 1:59 
GeneralRe: Macros are bad Pinmember[Deep]Xor21-Dec-05 2:26 
GeneralRe: Macros are bad Pinmember[Deep]Xor21-Dec-05 2:45 
GeneralRe: Macros are bad PinmemberAlexey Vasiliev10-Jan-06 20:49 
GeneralRe: Macros are bad Pinmember[Deep]Xor10-Jan-06 20:55 
GeneralSeems very familiar... PinmemberJason King19-Dec-05 8:34 
GeneralRe: Seems very familiar... PinmemberAchilleas Margaritis21-Dec-05 2:01 
GeneralRe: Seems very familiar... PinmemberJason King21-Dec-05 7:34 
GeneralRe: Seems very familiar... PinmemberZac Howland28-Dec-05 19:02 
General4 bytes per property is not &quot;no memory overhead&quot; PinmemberHal Angseesing19-Dec-05 5:42 
GeneralRe: 4 bytes per property is not &amp;quot;no memory overhead&amp;quot; PinmemberAchilleas Margaritis21-Dec-05 2:09 
QuestionDo you remember ... PinmemberSilentSilent19-Dec-05 5:20 
GeneralC++ is C++ Pinmembertoxcct19-Dec-05 4:04 
GeneralRe: C++ is C++ PinmemberNemanja Trifunovic19-Dec-05 4:29 
GeneralRe: C++ is C++ Pinmembertoxcct19-Dec-05 4:34 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 19 Dec 2005
Article Copyright 2005 by Achilleas Margaritis
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid