| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛

vector& operator=(const vector&); 求解释
class vector {
int sz;
double* elem;
void copy(const vector& arg);     // copy elements from arg into *elem
public:
vector(int s) :sz(s), elem(new double[s]) { } // constructor
~vector() { delete[] elem; }                  // destructor
// int size() const { return sz; }               // the current size
double get(int n) { return elem[n]; }         // access: read
void set(int n, double v) { elem[n]=v; }      // access: write
vector& operator=(const vector&);
};

vector& vector::operator=(const vector& a)
// make this vector a copy of a
{
double* p = new double[a.sz];     // allocate new space
copy(a);                          // copy elements
delete[ ] elem;                   // deallocate old space
elem = p;                         // now we can re-set elem
sz = a.sz;
return *this;
}

vector& operator=(const vector&); 那此时的operator只是一个vector&的变量，那它怎么又可以定义啊？？？

vector& vector::operator=(const vector& a)
// make this vector a copy of a
{
double* p = new double[a.sz];     // allocate new space
copy(a);                          // copy elements
delete[ ] elem;                   // deallocate old space
elem = p;                         // now we can re-set elem
sz = a.sz;
return *this;
}

if(this == &a) return *this;

• 6
• 1/1页
• 1

Powered by Discuz, Processed in 0.064490 second(s), 7 queries.