Property-like variable in C ++

I know that the implementation of properties-like objects has been discussed many times, but what I need is slightly different from the various solutions proposed.

I want an object that has the same semantics of a variable, but when its value changes, I want to perform some action. This means that there are implicit get / set methods that are implemented when the = operator and the implicit conversion operator are overloaded.

Note that when I mean property-like, I do not mean necessarily semantic object.property, but simply a way to have implicit get / set methods (as in property-like-variable = value). The reason is because I have some existing code that uses the variable, I want to perform some action every time the value of the variable changes without changing the existing code (only the declaration of the variable should change).

I got this result with a minimal code like this:

#include <stdio.h>

class Int
{
    int _value;

public:
    // default constructor
    Int() { printf("Int: initial value undefined\n"); }

    // assignment constructor
    Int(int value) : _value(value) { printf("Int: initial value is %d\n", _value); }

    // property get
    operator const int() const { return _value; }

    // property set
    int operator=(int value)
    { 
        _value = value;
        printf("new value is %d\n", value);
        return value;
    }

    // TODO: overload other operators if needed
};

This is a usage example:

class C
{
    Int _i;

public:
    C() : _i(2) { }

    void foo()
    {
        _i = 3;
        printf("foo(): _i = %d\n", _i);
    }
};

int main()
{
    int i;
    Int I;

    i = 1;
    I = i;
    I = I + 1;

    C c;
    c.foo();
}

Now a few questions:

  • Is this specific need already discussed? Have a design template?
  • Am I doing something bad here? (both in terms of implementation and in terms of design).
  • Is there any performance flaw with this, or is the compiler able to optimize it without overhead?
  • ? "" ( "" ).
  • ? , - / .
+5
2

Ad 2: :

  • const operator const int() (gcc )

  • Int& not int, , int, ++ :

    Int i;
    (i=2)=3;
    

Ad 3: set/get , / , . printf(), ;).

Ad 5. , :

template<typename T> class observe
{
    T _value;

public:
    // default constructor
    observe() { cout << "Int: initial value undefined" << endl; }

    // assignment constructor
    observe(const T& v) : _value(v) { cout << "initial value is " << v << endl; }

    // property get
    operator T() const { return _value; }

    // property set
    observe<T>& operator=(const T& value)
    {
        _value = value;
        cout << "new value is " << _value << endl;
        return *this;
    }
};

// special behavior for the assignment of doubles
template<> observe<double>& observe<double>::operator =(const double& value)
{
    _value = value;
    cout << "new double value is " << _value << endl;
    return *this;
}

observe<double> d;
d = 1.0;
+3

, , "set" class C. :

class C
{
public:
    class i_property_class {

...methods from your current Int class

    } i;

...rest of class C
}

, i_property_class , , , (, operator= ), .

5, , .

0

All Articles