让类与运算符一起工作更简单的方法
Easier way to make class to work with operators?
这里,我有一个名为Value
的类,它可以简单地获取和设置float
。
class Value
{
public:
Value(float f)
:f(f){};
float get()
{
return f;
}
void set(float f)
{
this->f = f;
}
private:
float f;
};
我希望我的班级能够像下面的例子一样工作。
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
我应该手动将所有运算符方法添加到我的类中吗?
或者有什么更简单的解决方案可以像治疗float
一样治疗Value
吗?
您可以使用float
类型的转换运算符来代替get()
:
operator float() const { return f; }
如果还希望启用更改值的操作(如/=
),可以使用类似的返回引用的非常数运算符,也可以手动添加这些运算符。
但是,如果您想要一个行为与float
完全相同的类,那么最好使用float
,而不是使用Value
类。
这里是相关算术、等式和流运算符的惯用实现。
内联注释中的注释。
另请参阅关于允许从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
}
我实现了/=
和==
运算符:
您可以使用此页面了解更多。。。https://www.tutorialspoint.com/cplusplus/cpp_overloading.htm
class Value
{
public:
Value(float f) : f(f) {};
operator float() const
{
return f;
}
void set(float f)
{
this->f = f;
}
Value &operator /=(float num) // e.g. value /= 2;
{
this->f = f / num;
}
bool operator==(const float& a) const // e.g. std::cout << value == 5 << std::endl; // -> true
{
if(this->f == a) return true;
return false;
}
private:
float f;
};
main:
int main()
{
Value value(10);
value /= 5;
cout << value << endl;
cout << (value == 5) << endl;
return 0;
}
相关文章:
- 查找定义我的 C/C++ 函数/宏的文件比'grep'更简单的方法
- 让类与运算符一起工作更简单的方法
- 多个 if-else 测试的更简单方法
- 为具有多个参数的函数创建 SWIG 类型图的更简单方法?
- 有没有更简单的方法可以从用户那里获取三个数字并按升序打印它们?
- 约束模板参数顺序的更简单方法
- C++ 将整数与硬编码的整数集进行比较的更简单方法
- 有没有一种方法可以在基于枚举的可变参数模板函数之间进行选择,这比将函数包装在结构中更简单
- 一种在没有 root 的情况下加载共享库的更简单方法
- 是否有一种更简单的方法可以使用GDB将C捕获的C结构捕获
- 生活的游戏邻居检查(更简单的方法可能吗?)
- 有没有更简单的方法可以做到:if(num1 > num2 && num1 > num3),以获得更大的变量列表进行比较?
- 有没有更简单的方法在 Rcpp 中使用 NumericVector 编写条件语句
- 访问派生属性和给定基指针的方法的更简单方法
- 为对象::tr传递QString的更简单方法
- 实现.cpp文件的更简单方法
- 为SPOJ NAKANJ编写条件块的更简单方法-最小骑士移动
- 让shared_ptr拥有现有指针的更简单方法
- 对字符串副本执行迭代器运算的更简单方法
- 计算矢量中相同字符串的更简单方法