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

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

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


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


struct EPLUS
{

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

class Matrix


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

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

~Matrix()
{

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


int getM() const
{ return M;}

int getN() const
{ return N;}

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

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

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

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

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

template <typename LeftExp, typename RightExp>

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


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

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

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