zoukankan      html  css  js  c++  java
  • 运算符重载

    加法还可用友元函数,两个参数的方式实现。

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    
    class Matrix
    {
    public:
        Matrix(){memset(this->_mat,0,sizeof(this->_mat));}
        Matrix(float tmp[4][4] )
        {
              for (int i=0;i<4;i++)
                          for(int j=0;j<4;j++)
                              _mat[i][j]=tmp[i][j];
        }
        Matrix(const Matrix *tmp)
        {
            memcpy(this->_mat,tmp->_mat,sizeof(tmp->_mat));
        }
    //    Matrix operator+(Matrix &a,Matrix &b)
    //    {
    //        Matrix result(a);
    //        return result+=b;
    //
    //    }
       Matrix operator+(Matrix &a)
       {
           Matrix tmp(this);
    
           tmp+=a;
           return tmp;
       }
      void operator+=(const Matrix &b) const
            {
          for (int i=0;i<4;i++)
                      for(int j=0;j<4;j++)
                          _mat[i][j]+=b._mat[i][j];
            }
        float& operator() (int , int );
        float operator() (int rwo,int colum) const;
        void print() const
        {
            for (int i=0;i<4;i++)
            {
                              for(int j=0;j<4;j++)
                              {
                                 cout<<_mat[i][j]<<" ";
                              }
            cout<<endl;
           }
        }
    private:
        mutable float _mat[4][4];
    };
    
    //float  Matrix::operator ()(int r,int c) const
    //{
    //    return this->_mat[r][c];
    //}
    float&  Matrix::operator ()(int r,int c)
    {
        return this->_mat[r][c];
    }
     
    int main()
    { 
        Matrix a,b;
        float tmp[4][4]={{0,1,2,3},{1,2,3,4},{2,3,4,5},{3,4,5,6}};
      Matrix aa(tmp);
      Matrix c(aa);
      c.print();
       cout<<c(1,3);
       c(1,3)=5; cout<<c(1,3)<<endl;
      b.print();
      aa+=aa;
      aa.print();
      a=aa+ b;
      a.print();
    
    
    }
  • 相关阅读:
    洛谷-P1855 榨取kkksc03
    Error: ORA-06502: PL/SQL: 数字或值错误 : character string buffer too small(触发器中使用系统动态视图导致)
    jwt ctf
    apktool+nuclei mobile
    Subdomain Takeover via Fastly ( Steps )
    乱七八糟
    Recon
    推荐几个我感觉不错的tips
    子域名收集
    清除Cookie的数据
  • 原文地址:https://www.cnblogs.com/huashiyiqike/p/3860978.html
Copyright © 2011-2022 走看看