Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;代码解释? 我从一本书中学习C++,有1行代码我不懂。该程序应该模拟ATM机前的排队人群。我不明白这句话: item = front->item; // Is it necessary and what does it do ?_C++ - Fatal编程技术网

C++;代码解释? 我从一本书中学习C++,有1行代码我不懂。该程序应该模拟ATM机前的排队人群。我不明白这句话: item = front->item; // Is it necessary and what does it do ?

C++;代码解释? 我从一本书中学习C++,有1行代码我不懂。该程序应该模拟ATM机前的排队人群。我不明白这句话: item = front->item; // Is it necessary and what does it do ?,c++,C++,它位于bool Queue::dequeue(Item&Item)的末尾。它必须与指针有关,但当我删除这一行时,程序工作正常 以下是代码的重要部分: class Customer { private: long arrive; int processtime; public: Customer() { arrive = processtime = 0; } void set(long when); long when() const { return a

它位于
bool Queue::dequeue(Item&Item)
的末尾。它必须与指针有关,但当我删除这一行时,程序工作正常

以下是代码的重要部分:

class Customer
{
private:
    long arrive; 
    int processtime;
public:
    Customer() { arrive = processtime = 0; }
    void set(long when);
    long when() const { return arrive; }
    int ptime() const { return processtime; }
};

typedef Customer Item;

class Queue
{
private:
    struct Node { Item item; struct Node * next; };
    enum { Q_SIZE = 10 };
    Node * front; 
    Node * rear; 
    int items; 
    const int qsize;
    Queue(const Queue & q) : qsize(0) { }
    Queue & operator=(const Queue & q) { return *this; }
public:
    Queue(int qs = Q_SIZE); 
    ~Queue();
    bool isempty() const;
    bool isfull() const;
    int queuecount() const;
    bool enqueue(const Item &item); 
    bool dequeue(Item &item); 
};

Queue::Queue(int qs) : qsize(qs)
{
    front = rear = NULL;
    items = 0;
}

Queue::~Queue()
{
    Node * temp;
    while (front != NULL) 
    {
        temp = front; 
        front = front->next; 
        delete temp; 
    }
}

bool Queue::enqueue(const Item & item)
{
    if (isfull())
        return false;
    Node * add = new Node; 
    if (add == NULL)
        return false; 
    add->item = item; 
    add->next = NULL;
    items++;
    if (front == NULL) 
        front = add; 
    else
        rear->next = add; 
    rear = add; 
    return true;
}

bool Queue::dequeue(Item & item)
{
    if (front == NULL)
        return false;
    item = front->item; // Is it necessary and what does it do ?
    items--;
    Node * temp = front; 
    front = front->next; 
    delete temp; 
    if (items == 0)
        rear = NULL;
    return true;
}

dequeue()
方法从队列中删除前端节点。在从队列中删除节点之前,该行将前端节点的
item
字段分配给
item
输出变量(通过引用传递)。

该行将前端元素的值分配给输出变量。如果没有它,您将弹出并永远失去元素。