简体   繁体   English

通用友元运算符== 重载

[英]Generic friend operator== overload

I am currently stuck on a problem that I can't solve.我目前被困在一个我无法解决的问题上。 I am a beginner in the world of c++.我是 c++ 世界的初学者。

For a homework, I have to create a generic class to represent a fraction like 5/6 or 11/4.对于家庭作业,我必须创建一个通用的 class 来表示像 5/6 或 11/4 这样的分数。 This class is generic which allows to determine the type of the nominator and numerator (unsigned short, unsigned, unsigned long).这个 class 是通用的,它允许确定提名者和分子的类型(无符号短、无符号、无符号长)。

The goal is to be able to add, subtract, multiply, divide and compare fractions.目标是能够加、减、乘、除和比较分数。 In a first phase, I created a class and operator overloads allowing me to do everything that is required but only for instances of the class with the same type.在第一阶段,我创建了 class 和运算符重载,允许我执行所需的所有操作,但仅适用于具有相同类型的 class 的实例。

Now I would like to improve the existing code to be able to perform operations between fractions of different types like for example: Frac<unsigned> == Frac<unsigned short> .现在我想改进现有代码,使其能够在不同类型的部分之间执行操作,例如: Frac<unsigned> == Frac<unsigned short> But there is something I don't understand in the syntax of overloading operators.但是在重载运算符的语法中有一些我不明白的地方。

Let's take the == operator as an example:我们以 == 运算符为例:

template <typename T>
bool operator==(const Frac<T>& lhs, const Frac<T>& rhs) {
  return (lhs.numerator == rhs.numerator && lhs.denominator == rhs.denominator);
}

template <typename T>
class Frac {

friend bool operator== <>(const Frac& lhs, const Frac& rhs);

private:
    T numerator, denominator;

};

this is the current version that worked for the Fracs of the same type.这是适用于相同类型 Frac 的当前版本。 But now I would like it to work for Fracs of a different type:但现在我希望它适用于不同类型的 Frac:

template <typename T>
class Frac;

template <typename T, typename U>
bool operator==(const Frac<T>& lhs, const Frac<U>& rhs){
return (lhs.numerator == rhs.numerator && lhs.denominator == rhs.denominator)
}

template <typename T>
class Frac {

friend bool operator== <>(const Frac& lhs, const Frac& rhs);


private:
    T numerator, denominator;

};

But I have a compilation error when I want to compare Frac of different types... I don't understand and didn't find any solution explaining concretely what the problem was and how to implement this kind of solution properly.但是当我想比较不同类型的 Frac 时出现编译错误......我不明白,也没有找到任何解决方案具体解释问题是什么以及如何正确实施这种解决方案。

Could you help me please?请问你能帮帮我吗?

I hope my question is clear and complete.我希望我的问题清楚而完整。

You can do this the following way:您可以通过以下方式执行此操作:

template <typename T>
class Frac {

//friend declaration
template <typename S, typename U> friend
bool operator==(const Frac<S>& lhs, const Frac<U>& rhs);


private:
    T numerator, denominator;

};
template <typename T, typename U>
bool operator==(const Frac<T>& lhs, const Frac<U>& rhs){
return ((lhs.numerator == rhs.numerator) && (lhs.denominator == rhs.denominator));
}

Now you will be able to compare Frac for different types as you want.现在,您将能够根据需要比较不同类型的Frac

I kept running into problems when I tried to implement the operator+.当我尝试实现 operator+ 时,我一直遇到问题。 The compiler told me that it was impossible to access a private field when I tried to add two Frac of different type.编译器告诉我,当我尝试添加两个不同类型的 Frac 时,无法访问私有字段。

In order to solve this problem, I made my class friendly with itself so that all instances of Frac can access the private fields of the other instances even if the type is different.为了解决这个问题,我让我的 class 对自己友好,这样即使类型不同,Frac 的所有实例都可以访问其他实例的私有字段。 Is this a correct solution?这是一个正确的解决方案吗?

template <typename T>
class Frac {

//make Frac self-friend to make all instance of Frac friends each others
// not depending of Frac T type
template<typename U>
friend class Frac;

friend std::ostream& operator<< <T>(std::ostream& o, const Frac& rhs);

friend Frac operator+ <>(Frac lhs, const Frac& rhs);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM