我知道类似属性的对象的实现已经讨论了很多次,但我需要的是与提出的各种解决方案略有不同.
我想要的是一个与变量具有相同语义的对象,但是当它的值被更改时,我想执行一些操作.这意味着具有隐式get/set方法,实现了重载operator =和隐式转换运算符.
请注意,当我的意思是"类似属性"时,我并不一定意味着语义object.property,而只是一种拥有隐式get/set方法的方法(如property-like-variable = value).原因是我有一些使用变量的现有代码,我想在每次变量值改变时执行一些操作而不改变现有代码(只有变量的声明应该改变).
我用这样的最小代码获得了这个结果:
#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
};
Run Code Online (Sandbox Code Playgroud)
这是一个示例用法:
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();
}
Run Code Online (Sandbox Code Playgroud)
现在有些问题:
广告问题 2:我发现您的代码存在两个常见问题:
声明中的第一个const限定符operator const int()毫无意义(gcc 对此发出警告)
赋值运算符的返回类型不应该是,Int&否则int它将具有与原始 int 不同的语义,并且以下 C++ 代码将无法编译:
Int i;
(i=2)=3;
Run Code Online (Sandbox Code Playgroud)广告问题 3:如果您的 set/get 函数除了分配/返回值之外什么都不做,那么它们应该被完全优化。如果你打电话printf(),你可能会严重陷入性能问题;)。
广告问题 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;
Run Code Online (Sandbox Code Playgroud)