【发布时间】:2019-03-03 14:11:57
【问题描述】:
所以我将 int 的变体定义为旋转整数类,这非常简单,但我希望能够做类似的事情
cout << x << '\n';
而不是:
cout << x.value() << '\n';
这有可能吗?
没有类似的
class rotating_int
{
private:
int _value, _low, _high;
public:
int operator++(int) { if (++_value > _high) _value = _low; return _value; }
int operator--(int) { if (--_value < _low) _value = _high; return _value; }
int operator++() { if (++_value > _high) _value = _low; return _value; }
int operator--() { if (--_value < _low) _value = _high; return _value; }
void operator=(int value) { _value = value; }
int operator==(int value) { return _value == value; }
int val() { return _value; }
rotating_int(int value, int low, int high) { _value = value; _low = low; _high = high; }
int ^rotating_array() { return &_value; }
};
其中 "^rotating_array" 很像析构函数 ~rotating_array 的定义。
似乎应该是面向对象设计的基础。
【问题讨论】:
-
您是否尝试过为您的班级重载运算符
-
是的。查找运算符重载。
标签: c++ overloading this instance operator-keyword