使班级与运营商合作更简单的方法?
问题描述:
在这里,我有一个名为Value
的类,它可以简单地获取并设置float
.
Here, I have a class called Value
which simply can get and set float
.
class Value
{
public:
Value(float f)
:f(f){};
float get()
{
return f;
}
void set(float f)
{
this->f = f;
}
private:
float f;
};
我希望我的班级能够像下面的示例一样工作.
And I want my class to be able to work like the following example.
Value value(3);
std::cout << value * 2 - 1 << std::endl; // -> 5
std::cout << value == 5 << std::endl; // -> true
value /= 2;
std::cout << value << std::endl; // -> 2.5
我应该手动将所有运算符方法添加到类中吗?
Should I manually add all operator methods to my class?
还是会有更简单的解决方案将Value
像float
一样对待?
Or would there be any easier solution to treat Value
like float
?
答
此处是相关算术,相等和流运算符的惯用实现.
Here is an idiomatic implementation of the relevant arithmetic, equality and stream operators.
内联注释中的注释.
另请参阅有关允许从float隐式转换的后果/好处的说明.
See also the note about the consequences/benefits of allowing implicit conversion from float.
#include <iostream>
class Value
{
public:
// Note - this constructor is not explicit.
// This means that in an expression we regard a float and a Value on the
// right hand side of the expression as equivalent in meaning.
// Note A.
// =
Value(float f)
:f(f){};
float get() const
{
return f;
}
void set(float f)
{
this->f = f;
}
// Idiom: unary operators defined as class members
//
Value& operator *= (Value const& r)
{
f *= r.f;
return *this;
}
Value& operator -= (Value const& r)
{
f -= r.f;
return *this;
}
Value& operator /= (Value const& r)
{
f /= r.f;
return *this;
}
private:
float f;
};
// Idiom: binary operators written as free functions in terms of unary operators
// remember Note A? A float will convert to a Value... Note B
// =
auto operator*(Value l, Value const& r) -> Value
{
l *= r;
return l;
}
auto operator-(Value l, Value const& r) -> Value
{
l -= r;
return l;
}
auto operator<<(std::ostream& l, Value const& r) -> std::ostream&
{
return l << r.get();
}
// Idiom: binary operators implemented as free functions in terms of public interface
auto operator==(Value const& l, Value const& r) -> bool
{
return l.get() == r.get();
}
int main()
{
Value value(3);
// expressions in output streams need to be parenthesised
// because of operator precedence
std::cout << (value * 2 - 1) << std::endl; // -> 5
// ^^ remember note B? value * 2 will resolve to value * Value(2) because of
// implicit conversion (Note A)
std::cout << (value == 5) << std::endl; // -> true
value /= 2;
std::cout << value << std::endl; // -> 2.5
}