#include <iostream>
#include <cstring>
using namespace std;
class Matrix
{
public:
Matrix(double d = 1.0)
{
cout << "Matrix::Matrix()" << endl;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
m[i][j] = d;
}
}
}
Matrix(const Matrix& mt)
{
cout << "Matrix::Matrix(const Matrix&)" << endl;
memcpy(this, &mt, sizeof(Matrix));
}
Matrix& operator=(const Matrix& mt)
{
if (this == &mt)
{
return *this;
}
cout << "matrix::operator=(const Matrix&)" << endl;
memcpy(this, &mt, sizeof(Matrix));
return *this;
}
friend const Matrix operator+(const Matrix&, const Matrix&);
protected:
private:
double m[10][10];
};
const Matrix operator+(const Matrix& arg1, const Matrix& arg2)
{
Matrix sum;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
sum.m[i][j] = arg1.m[i][j] + arg2.m[i][j];
}
}
return sum;
}
int main()
{
Matrix a(2.0), b(3.0), c;
c = a + b;
system("pause");
return 0;
}
[C++应用程序性能优化]临时对象
最新推荐文章于 2025-07-09 10:27:00 发布
本文介绍了矩阵类的构造函数、拷贝构造函数、赋值运算符重载以及矩阵加法运算实现,同时展示了如何使用memcpy进行内存复制。
3441

被折叠的 条评论
为什么被折叠?



