Click here to Skip to main content
Licence 
First Posted 19 Dec 2005
Views 52,096
Bookmarked 13 times

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

By | 19 Dec 2005 | Article
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

About the Author

Achilleas Margaritis

Software Developer (Senior)

Greece Greece

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalit's very good [modified] Pinmemberztf76222:32 22 Aug '06  
GeneralI like it PinmemberSteven Burns17:35 5 Mar '06  
GeneralSeriously Pinmemberarmentage4:03 20 Dec '05  
GeneralRe: Seriously PinmemberAchilleas Margaritis1:57 21 Dec '05  
GeneralRe: Seriously Pinmemberarmentage3:30 21 Dec '05  
GeneralRe: Seriously PinmemberSSacek10:58 27 Dec '05  
GeneralRe: Seriously Pinmemberarmentage11:08 27 Dec '05  
GeneralRe: Seriously PinmemberSSacek11:25 27 Dec '05  
GeneralRe: Seriously PinmemberZac Howland18:56 28 Dec '05  
GeneralMacros are bad Pinmember[Deep]Xor2:21 20 Dec '05  
GeneralRe: Macros are bad PinmemberAchilleas Margaritis1:59 21 Dec '05  
GeneralRe: Macros are bad Pinmember[Deep]Xor2:26 21 Dec '05  
GeneralRe: Macros are bad Pinmember[Deep]Xor2:45 21 Dec '05  
GeneralRe: Macros are bad PinmemberAlexey Vasiliev20:49 10 Jan '06  
GeneralRe: Macros are bad Pinmember[Deep]Xor20:55 10 Jan '06  
GeneralSeems very familiar... PinmemberJason King8:34 19 Dec '05  
GeneralRe: Seems very familiar... PinmemberAchilleas Margaritis2:01 21 Dec '05  
GeneralRe: Seems very familiar... PinmemberJason King7:34 21 Dec '05  
GeneralRe: Seems very familiar... PinmemberZac Howland19:02 28 Dec '05  
General4 bytes per property is not "no memory overhead" PinmemberHal Angseesing5:42 19 Dec '05  
GeneralRe: 4 bytes per property is not &quot;no memory overhead&quot; PinmemberAchilleas Margaritis2:09 21 Dec '05  
QuestionDo you remember ... PinmemberSilentSilent5:20 19 Dec '05  
GeneralC++ is C++ Pinmembertoxcct4:04 19 Dec '05  
GeneralRe: C++ is C++ PinmemberNemanja Trifunovic4:29 19 Dec '05  
GeneralRe: C++ is C++ Pinmembertoxcct4:34 19 Dec '05  

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.

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