来自另一个类的模板运算符专门化

时间:2015-01-22 18:41:56

标签: c++

所以我试图改变operator+方法的行为,因为我需要在另一个类的方法中实现更少的方法。 如果有人在这里给我一个以下一个或多个的手,我会非常感激:

  1. 也许我一直在寻找错误?如果是这样,那么指向正确答案或信息来源的链接就会很棒。
  2. 编写类似内容的一般方法?
  3. 方案的一些代码示例:

    class Rational
    {
    public:
        const Rational Rational::operator+(const Rational &other) const {
            Rational ans;
            ans._denominator = other.getDenominator() * getDenominator();
            ans._numerator = getNumerator() * other.getDenominator() +
                other.getNumerator() * getDenominator();
            ans.init_rational(); /* <-- This part formats the rational number
                                        every time so that it'd look like 1/2
                                        instead of 2/4(f.e). The purpose of the
                                        specialized method in the other class is
                                        to perform the trace() method where lots
                                        of x+x+x is performed, therefore it'd be
                                        slow and unnecessary to use
                                        "init_rational()" before it's done adding
                                 */
            return ans;
        }
    };
    

    需要专业运算符+的类:

    template <class T>
    class Matrix
    {
    private:
        int rows, cols;
        vector<vector<T>> mat;
    public:
        const bool trace(T& ans) const
        {
            if (_rows() != _cols())
            {
                ans = T();
                return false;
            }
            ans = T();
            for (int i = 0; i < _rows(); i++)
            {
                ans = ans + mat[i][i];
            }
            return true;
        }
    }
    
    顺便说一句,我想我想要完成的事情可以在没有专门针对整个Rational类型的专业化的情况下完成,而不是这里的实际答案吗?或者我正在寻找一个选项呢?

    P.S:如果您觉得需要更多信息/方法,请问:p

    编辑: 从我到这里的回答中我想我不应该按照我想要的方式去做,但做这样的事情呢?

    template <Rational>
        const bool trace(Rational& ans) const{
            if (_rows() != _cols()){
                ans = Rational();
                return false;
            }
            ans = Rational();
            for (int i = 0; i < _rows(); i++){
                //adding method implemented here or in a separate function in the Rational class
            }
            return true;
        }
    

2 个答案:

答案 0 :(得分:1)

你在问题​​的第一部分所要求的确实没有像Barry所说的那样有意义,考虑到你编辑的信息和评论,这可能是你想要的吗?

bool trace(Rational& ans) const{return true;}

然后

template<typename T>
bool trace(T& ans) const{return false;}

如果是这样,你已经非常接近,只需要将模板声明置于顶部并作为泛型类型而不是相反于您倾向于特定类型的其他方式:)

答案 1 :(得分:0)

不,在operator+(Rational, Rational)的上下文中调用时,Matrix<Rational>不能使某些内容与从其他来源调用的内容不同。这没有多大意义,严重违反任何封装概念。

如果您需要Matrix内的特殊逻辑

ans = ans + mat[i][i];

然后,您可以提供另一层间接,以便Matrix内部可以为Rational所需的内容做正确的事情:

increment(ans, mat[i][i]);

其中:

template <typename T>
class Matrix {
private: // <--
    template <typename U>
    void increment(U& lhs, const U& rhs) {
        lhs += rhs;
    }

    void increment(Rational& lhs, const Rational& rhs) {
        // do something else
    }
};
相关问题