C++ 在一个函数中重载多个运算符
是否可以在一个操作员过载中过载多个操作员 我想这样做:C++ 在一个函数中重载多个运算符,c++,oop,operator-overloading,C++,Oop,Operator Overloading,是否可以在一个操作员过载中过载多个操作员 我想这样做: template<typename L, typename R> auto operator+(const Point_impl<L>& lhs, const Point_impl<R>& rhs) { // return the properly promoted Point_impl type deduced from if the // underlying type
template<typename L, typename R>
auto operator+(const Point_impl<L>& lhs, const Point_impl<R>& rhs) {
// return the properly promoted Point_impl type deduced from if the
// underlying types were added
Point_impl<decltype(lhs.get_x() + rhs.get_x())> rv = lhs;
rv += rhs;
return rv;
}
template<typename L, typename R>
auto operator-(const Point_impl<L>& lhs, const Point_impl<R>& rhs) {
Point_impl<decltype(lhs.get_x() - rhs.get_x())> rv = lhs;
rv -= rhs;
return rv;
}
template<typename L, typename R>
auto operator*(const Point_impl<L>& lhs, const Point_impl<R>& rhs) {
Point_impl<decltype(lhs.get_x() * rhs.get_x())> rv = lhs;
rv *= rhs;
return rv;
}
template<typename L, typename R>
auto operator/(const Point_impl<L>& lhs, const Point_impl<R>& rhs) {
Point_impl<decltype(lhs.get_x() / rhs.get_x())> rv = lhs;
rv /= rhs;
return rv;
}
模板
MyClass运算符(常量MyClass和其他){
返回此.x其他.x;
}
其中x是类的int属性
这样我就可以一次重载所有的int运算符,这意味着我可以用一个函数重载+,-,*,/等等,而不是复制粘贴一个代码无数次,只更改一行
在我的具体示例中,我有一个包含两个整数的结构点和另一个名为Pointf的结构,它包含两个浮点数,我希望能够将它们相互相加、子结构、相乘和微分,或者使用一个int值,目前我用十几个函数来实现,但我只想用几个函数来实现,我还想覆盖+=、-=、*-和/=运算符。不,不能在一个运算符重载中重载多个运算符。但是,您可以制作一个类模板,用于所有
点
s:
#include <iostream>
template<typename T>
class Point_impl {
public:
using value_type = T;
template <typename U>
friend class Point_impl;
Point_impl() : x_{}, y_{} {} // default
Point_impl(T x, T y) : x_{x}, y_{y} {} // conversion
// create one Point_impl<T> from a Point_impl<U>
template<typename U>
Point_impl(const Point_impl<U>& rhs) :
x_{static_cast<T>(rhs.x_)},
y_{static_cast<T>(rhs.y_)}
{}
T get_x() const { return x_; }
T get_y() const { return y_; }
// add a Point_impl<U> to *this
template<typename U>
Point_impl<T>& operator+=(const Point_impl<U>& rhs) {
x_ += static_cast<U>(rhs.x_);
y_ += static_cast<U>(rhs.y_);
return *this;
}
template<typename U>
Point_impl<T>& operator-=(const Point_impl<U>& rhs) {
x_ -= static_cast<U>(rhs.x_);
y_ -= static_cast<U>(rhs.y_);
return *this;
}
template<typename U>
Point_impl<T>& operator*=(const Point_impl<U>& rhs) {
x_ *= static_cast<U>(rhs.x_);
y_ *= static_cast<U>(rhs.y_);
return *this;
}
template<typename U>
Point_impl<T>& operator/=(const Point_impl<U>& rhs) {
x_ /= static_cast<U>(rhs.x_);
y_ /= static_cast<U>(rhs.y_);
return *this;
}
// for printing a point:
friend std::ostream& operator<<(std::ostream& os, const Point_impl<T>& p) {
return os << '(' << p.x_ << ',' << p.y_ << ')';
}
private:
T x_, y_;
};
#包括
模板
类点\u impl{
公众:
使用值_type=T;
模板
朋友类点;
Point_impl():x_{},y_{}{}//默认值
点_impl(tx,ty):x{x},y{y}{}//转换
//从点导入创建一个点导入
模板
施工点(施工点和右侧):
x{static_cast(rhs.x_)},
y{static_cast(rhs.y)}
{}
T get_x()常量{return x_;}
T get_y()常量{return y;}
//向*添加一个点
模板
点输入和运算符+=(常量点输入和rhs){
x_uu+=静态_u铸造(rhs.x_u);
y_u+=静态_u投射(rhs.y_u);
归还*这个;
}
模板
点输入和运算符-=(常量点输入和rhs){
x_uu-=静态_u铸件(rhs.x_u);
y_uu-=静态_u铸造(rhs.y_u);
归还*这个;
}
模板
点输入和运算符*=(常数点输入和rhs){
x_u*=静态_u铸件(rhs.x_*);
y_u*=静态_u铸造(rhs.y_u);
归还*这个;
}
模板
点输入和运算符/=(常数点输入和rhs){
x_u/=静态_u铸件(rhs.x_u);
y_u/=静态_u铸造(rhs.y_u);
归还*这个;
}
//要打印点,请执行以下操作:
friend std::ostream&operatorCRTP可以避免从一个类到另一个类的一些重复,但不使用运算符的“自动”投影。您可以使用预处理器宏串联来组合运算符
和op
。简短回答:否。比较运算符(自C++20)是一种特殊情况,其中支持spaceship运算符,在某些特定情况下支持自动生成。但没有任何其他数学运算符的隐式生成。解决方法是使用宏,其中宏展开显式定义一组运算符(例如运算符+()
,运算符+=()
等)。
using Point = Point_impl<int>;
using Pointl = Point_impl<long>;
using Pointf = Point_impl<float>;
using Pointd = Point_impl<double>;
using Pointld = Point_impl<long double>;
#include <type_traits>
int main()
{
Point x(1,2); // Point_impl<int>
Pointf y(3.141f,6.282f); // Point_impl<float>
auto z = x + y; // int + float => Point_impl<float>
std::cout << std::is_same<decltype(z), Pointf>::value << '\n'; // prints 1
std::cout << z << '\n'; // (4.141,8.282)
}