| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
 Reworld，下班在家制作游戏，1500万奖金等你拿 以码会友 以友辅仁

已结贴   问题点数：10  回复次数：5
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&);
};

得分:5

得分:0
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;
}

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

得分:0

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