班级经营者 [英] Class operators

查看:78
本文介绍了班级经营者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having a problem to make the code:

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;
};

我怎样才能使一位操作员能够帮助我完成遇到问题的生产线?

How can I make an operator that will help me to do the line that I'm having a problem with?

推荐答案

没有operator [][],但有operator[].这样一来,您应该返回也可以使用[]的内容(指针或代理类).

There are no operator [][], but operator[]. So that one should return something for which you can use [] too (pointer or proxy class).

就您而言,您可以简单地这样做:

In your case, you might simply do:

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

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

For more complicated cases, you have to return a proxy class.

这篇关于班级经营者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆