这是上次PPT所应该讲述的代码内容。现在放上来,大家就应该清楚PPT未完成的部分所想要描述的技术了。PS:代码中还是有些没用的东西。
 template <typename LeftExp, typename Op, typename RightExp>
template <typename LeftExp, typename Op, typename RightExp>

 struct Expression
struct Expression  {
{
 const LeftExp & left;
    const LeftExp & left;
 const RightExp & right;
    const RightExp & right;
 
    

 Expression(const LeftExp & _l, const RightExp & _r) : left(_l), right(_r)
    Expression(const LeftExp & _l, const RightExp & _r) : left(_l), right(_r) {}
{}

 
    

 double operator [] (int index) const
    double operator [] (int index) const  {
{
 return Op::eval(left[index], right[index]);
        return Op::eval(left[index], right[index]);
 }
    }
 };
};


 struct EPLUS
struct EPLUS  {
{

 static double eval(double a, double b)
    static double eval(double a, double b)  { return a+b;}
{ return a+b;}
 };
};

 class Matrix
class Matrix 


 {
{
 private:
private:
 int M,N;
    int M,N;
 double * element;
    double * element;
 public:
public:

 Matrix(int m, int n): M(m),N(n)
    Matrix(int m, int n): M(m),N(n)  {
{
 element = new double[M*N];
        element = new double[M*N];
 }
    }

 ~Matrix()
    ~Matrix() {
{

 if (element)
        if (element)  {
{
 delete []element;
            delete []element;
 element = 0;
            element = 0;
 }
        }
 }
    }


 int getM() const
    int getM() const  { return M;}
{ return M;}

 int getN() const
    int getN() const  { return N;}
{ return N;}

 int getSize() const
    int getSize() const  {return M*N;}
{return M*N;}
 
    

 double & operator ()(int i, int j) const
    double & operator ()(int i, int j) const  { return element[i*N+j]; }
{ return element[i*N+j]; }

 double operator [](int index) const
    double operator [](int index) const  {return element[index]; }
{return element[index]; }
 
    
 template <typename Exp>
    template <typename Exp>

 Matrix & operator = (Exp & exp)
    Matrix & operator = (Exp & exp)  {
{    
 int size = M*N;
        int size = M*N;

 for (int i=0;i<size;i++)
        for (int i=0;i<size;i++)  {
{
 element[i] = exp[i];
            element[i] = exp[i];
 }
        }
 return * this;
        return * this;
 }
    }

 template <typename LeftExp, typename RightExp>
template <typename LeftExp, typename RightExp>

 inline Expression<LeftExp,EPLUS, RightExp> operator + (const LeftExp & a, const RightExp & b)
inline Expression<LeftExp,EPLUS, RightExp> operator + (const LeftExp & a, const RightExp & b)  {
{ 
 return Expression<LeftExp, EPLUS, RightExp>(a, b);
    return Expression<LeftExp, EPLUS, RightExp>(a, b);
 }
}


 int main()
int main()  {
{
 const int M=3, N=4;
    const int M=3, N=4;
 Matrix a(M,N), b(M,N), c(M,N);
    Matrix a(M,N), b(M,N), c(M,N);

 
     {
{
 for (int i=0;i<M;i++)
        for (int i=0;i<M;i++) 

 for (int j=0;j<N;j++)
            for (int j=0;j<N;j++)  {
{
 a(i,j) = i+j;
                a(i,j) = i+j;
 b(i,j) = i*j;
                b(i,j) = i*j;
 c(i,j) = 0;
                c(i,j) = 0;
 }
            }
 }
    }
 c = a + b;
    c = a + b;
 return 0;
    return 0;
 }
}