【问题标题】:Class operators类运算符
【发布时间】:2018-12-28 00:13:16
【问题描述】:

我在编写代码时遇到问题:

void main(){
     Matrix c(rows,cols);//rows & cols are int numbers
     c[0][0]=2//the line that I'm having a problem to do the operator
}

//My class defined like this: 
class Matrix{
public:
     Matrix(int rows,int cols): rows(rows), cols(cols){
         mat= new double*[cols];
         for( int i=0; i<rows;i++){
             *mat=new double[i];
         }
     }
private:
     int rows,cols;
     double **mat;
};

我怎样才能创建一个操作员来帮助我完成我遇到问题的生产线?

【问题讨论】:

  • 您应该注意 void main 不是有效的 C++。只有int main,所以最好使用它,因为a)这是C++规则规定的,b)无论如何写起来更短
  • @MooingDuck 它在构造函数体中。短语“方法”在 C++ 中没有特殊含义,但我认为在通俗意义上我们可以将构造函数等特殊成员函数视为“方法”。
  • 您可以使用double&amp; Matrix::operator()(int r, int c); 方式访问矩阵中的元素。

标签: c++ class operator-overloading


【解决方案1】:

没有operator [][],而是operator[]。所以应该返回一些你也可以使用[]的东西(指针或代理类)。

在你的情况下,你可以简单地这样做:

double* operator[](int i) { return mat[i]; }
const double* operator[](int i) const { return mat[i]; }

对于更复杂的情况,你必须返回一个代理类。

【讨论】:

    【解决方案2】:

    不要像那样在二维中动态分配。这是poison for your cache, and completely pointless。我看到了all the time,但我希望我没有!让自己成为一个漂亮的std::vector&lt;double&gt;,大小为rows*cols

    无论如何,允许[width][height] 的诀窍是代理类。让您的 operator[] 返回一个具有自己的 operator[] 的类的实例以进行二级查找。

    类似这样的:

    #include <iostream>
    #include <vector>
    
    struct Matrix
    {
        Matrix(const size_t columns, const size_t rows)
           : columns(columns)
           , rows(rows)
           , data(columns*rows, 0)
        {}
    
        size_t index(const size_t x, const size_t y) const
        {
            return x + y*columns;
        }
    
        double& at(const size_t x, const size_t y)
        {
            return data[index(x, y)];
        }
    
        double at(const size_t x, const size_t y) const
        {
            return data[index(x, y)];
        }
    
        template <bool Const>
        struct LookupHelper
        {
            using ParentType = std::conditional_t<Const, const Matrix, Matrix>;
            using ReturnType = std::conditional_t<Const, double, double&>;
    
            LookupHelper(ParentType& parent, const size_t x) : parent(parent), x(x) {}
    
            ReturnType operator[](const size_t y)
            {
                return parent.data[parent.index(x, y)];
            }
    
            const ReturnType operator[](const size_t y) const
            {
                return parent.data[parent.index(x, y)];
            }
    
        private:
            ParentType& parent;
            const size_t x;
        };
    
        LookupHelper<false> operator[](const size_t x)
        {
            return {*this, x};
        }
    
        LookupHelper<true> operator[](const size_t x) const
        {
            return {*this, x};
        }
    
    private:
        const size_t columns, rows;
        std::vector<double> data;
    };
    
    int main()
    {
        Matrix m(42, 3);
        m[15][3] = 1;
        std::cout << m[15][3] << '\n';
    }
    

    (实际上,您希望它可以移动,并且毫无疑问可以整理一下。)

    当然,switching to operator()a .at(width, height) member function 更容易……

    【讨论】:

    • stackoverflow.com/questions/49822848/… 有该代理类的示例。
    • @Jarod42 我在某个地方有一个非常好的但找不到它:( 而是重新创建了一个穷人的版本
    • 很确定也有 dup,但找不到,[] 可能在搜索中被解释为空标签:-/
    • @Jarod42 甚至懒得尝试使用符号搜索 ^_^ 我刚刚使用了很多关于“矩阵”“二维向量”“二维向量”“外观”主题的变体““索引”“索引”“代理类”“帮助类型”等等这些年来我找不到我的宝石:(
    • @Jarod42 啊,it was on a deleted question #sadface 不管怎样,它看起来和这个差不多:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    • 2011-08-18
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多