使用赋值运算符重载时,无法在operator =()之外访问新对象

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)

Sho*_*hoe 5

operator=()应该如何看待功能?

一个operator=应该修改内部对象(*this)与的值rhs,然后返回字面上(*this)