Click here to Skip to main content
15,885,216 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 94.5K   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

 
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 
GeneralRe: C++ is C++ Pin
toxcct19-Dec-05 4:34
toxcct19-Dec-05 4:34 
JokeRe: C++ is C++ Pin
Shawn Poulson19-Dec-05 4:46
Shawn Poulson19-Dec-05 4:46 
GeneralRe: C++ is C++ Pin
toxcct19-Dec-05 4:55
toxcct19-Dec-05 4:55 
GeneralRe: C++ is C++ Pin
Achilleas Margaritis19-Dec-05 5:13
Achilleas Margaritis19-Dec-05 5:13 
GeneralRe: C++ is C++ Pin
toxcct19-Dec-05 5:15
toxcct19-Dec-05 5:15 
GeneralRe: C++ is C++ Pin
Achilleas Margaritis21-Dec-05 2:10
Achilleas Margaritis21-Dec-05 2:10 
Let me trust boost developers more than you though...Smile | :)

GeneralRe: C++ is C++ Pin
Nemanja Trifunovic19-Dec-05 5:22
Nemanja Trifunovic19-Dec-05 5:22 
GeneralRe: C++ is C++ Pin
Rage20-Dec-05 4:17
professionalRage20-Dec-05 4:17 
GeneralRe: C++ is C++ Pin
Achilleas Margaritis19-Dec-05 5:12
Achilleas 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.