0

Thinking about a way to simulate C#'s properties in C++, I came to the following solution:

#include <iostream>

class obj_with_property {
private:
    class mykey {};

public:
    class int_property {
    private:
        int m_v;
    public:
        int_property (int v, mykey) : m_v (v) {
        }

        int_property & operator = (int v) {
            m_v = v;
            return * this;
        }

        operator int () const {
            return m_v;
        }
    };

    int_property A;

    obj_with_property () : A (int_property (0, mykey ())) {
    }
};

int main(int argc, char **argv) {
    obj_with_property obj;
    std::cout << obj.A << std::endl;
    obj.A = 25;
    std::cout << obj.A << std::endl;
    return 0;
}

I guess this approach could be improved further, e.g. by making int_property a template etc. Now I cannot imagine I'm the first one to have this idea. Does anybody know whether a similar approach has been discussed anywhere?

JohnB
  • 13,315
  • 4
  • 38
  • 65
  • I did that kind of things, in supported default values and hierarchical properties, and was templated. I also once put support for setter (so that `property = x` will call a function), but since it was not used in my project I dropped it. I think your approach is on the right way. – Synxis Dec 25 '13 at 08:57
  • Question: "Does anybody know whether a similar approach has been discussed anywhere?" In that case, I do not have to re-invent the wheel. – JohnB Dec 25 '13 at 08:59
  • NO. I do not want to *discuss* how properties can be simulated in C++. My exact question is whether any of the various C++ gurus has discussed this particular approach anywhere in one of the many C++ books or blogs. – JohnB Dec 25 '13 at 09:11
  • 1
    It's even on Wikipedia, yet without citation, unluckily. http://en.wikipedia.org/wiki/Property_%28programming%29#C.2B.2B – JohnB Dec 25 '13 at 09:20

1 Answers1

4

Actually you could do it easily using templates - Here is a basic implementation: Now I saw in your comments that you don't want such an answer as this one so this is for the benefits of others.

I myself would not use this approach since in most libraries I create I don't allow users to directly create objects - so the following approach will not work without further wrappers.

template<class T>
class property
{
    T value_;
public:
    property(){}
    property(T v) : value_(v){}
    property(property<T> const & other) : value_(other.value_){}

    property<T> & operator=(property<T> const& other)
    {
        value_ = other.value_;
        return *this;
    }

    operator T(){return value_;}
};

class object_with_properties
{
public:
    object_with_properties(){}

    property<int> intP;
    property<double> doubleP;
    property<std::string> strP;
};

Now you can use it in your code the following way:

object_with_properties o;
o.intP = 1;
o.doubleP = 1.0;
o.strP = std::string("1");

Now this code has its limitations since T must have a default constructor exposed but it should work for most cases where you allow users to create objects directly.

Sebastian Cabot
  • 1,812
  • 14
  • 18