2012-07-30 29 views
2

我最近讀了larbin源代碼。但我有疑問。我的global.h文件中定義的全局類,在這個文件的末尾有這樣的宏:爲什麼用這種方式調用宏?

#define setPoll(fds, event) \ 
global::pollfds[global::posPoll].fd = fds; \ 
global::pollfds[global::posPoll].events = event; \ 
global::posPoll++ 

但在取/ fetchPipe.cc文件,調用此宏是這樣的:

global::setPoll(n, POLLOUT); 

問題是爲什麼使用global ::來調用這個宏?我覺得使用

setPoll(n, POLLOUT); 

沒問題。任何人都可以告訴我爲什麼?

+4

'global :: setPoll'對我來說看起來像一個bug。它會擴展到'global :: global :: pollfds ['... – ugoren 2012-07-30 12:48:15

+0

是'C++'還是'c'? – 2012-07-30 12:51:38

+2

@eharvest C++,C不接受'global :: whatever'。 – 2012-07-30 12:55:18

回答

1

源代碼是一個完整的混亂,它甚至不會編譯。似乎global.h已在版本2.2.2和當前版本2.6.3之間進行了更改,但未解決fetch/fetchPipe.h中的這些更改。也有看看那些包括語句global.cc

#include <iostream.h> // iostream.h? 
#include <unistd.h> // twice, see below 
#include <errno.h> 
#include <string.h> // mixing C++ and C libraries 
#include <sys/types.h> 
#include <unistd.h> 
    ... 

此代碼已經過時,而不是標準的C++。還有其他一些錯誤。但回到你的問題:是的,setPoll(n, POLLOUT);應該是足夠的。使用global::setPoll不會導致一個錯誤,因爲這將擴大到

global::global::pollfds[global::posPoll].fd = fds; 
global::pollfds[global::posPoll].events = event; 
global::posPoll++; 

globalstruct(見Mike Seymour's comment)。

+0

你是對的,它不是標準的C++。我驚訝的是,它可以在gcc版本4.5.2(Ubuntu/Linaro 4.5.2-8ubuntu4)上用global :: setPoll(n,POLLOUT)編譯OK – laifjei 2012-07-30 13:20:02

+1

'global :: global :: pollfds'不是一個錯誤,如果'全球'是一個類;一個類名被注入類的範圍,所以'global','global :: global'和'global :: global :: global :: global :: global'都是等價的。 – 2012-07-30 13:27:52

+0

@MikeSeymour:偶然誤讀了「global」的聲明,並認爲它是一個命名空間。我的錯。 – Zeta 2012-07-30 13:35:12

相關問題