2015-10-21 38 views
0

我正在使用自定義鏈接列表類的代碼。 List類有以下功能:將指針變量設置爲多個值

void linkedList::expire(Interval *interval, int64 currentDt) 
{ 
    node *t = head, *d; 
    while (t != NULL) 
    { 
     if (t->addedDt < currentDt - (interval->time + (((long long int)interval->month)*30*24*3600*1000000))) 
     { 
      // this node is older than the expiration and must be deleted 
      d = t; 
      t = t->next; 

      if (head == d) 
       head = t; 

      if (current == d) 
       current = t; 

      if (tail == d) 
       tail = NULL; 

      nodes--; 
      //printf("Expired %d: %s\n", d->key, d->value); 
      delete d; 
     } 
     else 
     { 
      t = t->next; 
     } 
    } 
} 

我不明白的是代碼的函數的第一行:

node *t = head, *d; 

它是如何,該代碼編譯?如何將兩個值分配給單個變量,或者這是一些簡寫快捷方式? head是類型爲* node的成員變量,但在其他地方找不到d。

回答

4

這些是兩個定義,不是comma operator 。它們等同於

node* t = head; 
node* d; 

逗號操作符在C++所有運營商的優先級最低,所以調用它需要括號:

node* t = (head, *d); 

這將正常工作,如果d是類型爲node**

0

一般在C++中,你可以列出多個定義,它們之間用逗號分隔:

int a,b,c,d; 

將確定4個整數。危險的是,指針沒有的方式,可能是顯而易見的處理:

int* a,b,c,d; 

會宣佈是一個指向int,剩下的將只是整數。因此,在風格中聲明指針的情況並不少見:

int *a, *b; 

它聲明兩個整數指針。