Pab*_*eco -1 c++ operator-overloading
我有一个Queue的实现,需要编写一个赋值运算符重载。
Queue& Queue::operator= (const Queue& rhs){
if(this->head == rhs.head) return *this;
Queue * newlist;
if(rhs.head == NULL){
// copying over an empty list will clear it.
this->clear();
return * newlist;
}
newlist = new Queue(rhs);
cout << "made new queue" << endl;
cout << "new list : " << * newlist << endl;
return * newlist;
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当我离开此功能时,的内容newlist将不再可访问。operator =()函数应该看起来如何?
编辑:queue.h:
class Queue : public LinkedList {
protected:
unsigned maxSize;
public:
Queue(unsigned N = -1);
Queue(const Collection& collection, unsigned N = -1);
~Queue();
Queue(const Queue& obj);
Queue& operator= (const Queue& rhs);
friend std::ostream& operator<<(std::ostream& ostream, const Queue &rhs);
bool add(myType element);
myType element();
bool offer(myType element);
myType peek();
myType poll();
myType remove();
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
62 次 |
| 最近记录: |