#include <iostream>
using std::cout;
const double A1[] = {0.0, 1.0, 2.0, 3.0, 4.0};
const double A2[] = {1.0, 1.0, 1.0, 1.0, 1.0};
class Vector {
int m_n;
double *m_v;
Vector() {}
void copy(const Vector&v) {
m_n = v.m_n;
m_v = new double[v.m_n];
for(int i=m_n; i--; ) m_v[i] = v.m_v[i];
}
public:
Vector(const unsigned n, const double*const v) {
m_n = n;
m_v = new double[n];
for(int i=0;i<n;i++) m_v[i] = v[i];
}
Vector(const Vector& v) {
copy(v);
}
~Vector() {
delete m_v;
cout << "Destructing\n";
}
friend Vector add(const Vector& a, const Vector &b);
friend std::ostream& operator<<(std::ostream&, const Vector&);
};
Vector add(const Vector& a, const Vector& b) {
if (a.m_n!=b.m_n) {cout << "Error 2 vectors\n"; exit(-1);}
Vector res(a);
// Vector tmp(a); this still calls the destructor when exit, as expected
for (int i=0;i<a.m_n;i++)
res.m_v[i] += b.m_v[i];
return res;
}
std::ostream& operator<<(std::ostream&os, const Vector&v) {
os<<"{ ";
for(unsigned i=0; i<v.m_n; ++i)
os << v.m_v[i] << " ";
os<<"}";
return os;
}
int main()
{
Vector a(5,A1), b(5,A2);
Vector c = add(a,b);
cout << c;
int n;
std::cin >> n;
return 0;
}
要查看的主要内容是函数:void copy(const Vector& v)
,~Vector()
和Vector add(...)
在add()中,我们创建一个临时的 Vector res(a); 。此功能return res;
但是 res 不是局部变量吗?函数返回时为什么不被破坏(调用其析构函数)?如果我创建另一个变量 Vector tmp(a); ,它会被破坏。那么为什么 res 变量不会被破坏呢?关于函数返回我遗漏的对象的东西吗?还是与编译器相关? 谢谢您阅读