2014-09-22 126 views
0

在我的.cpp程序的.h文件中,我有以下內容。
FILE *sequence_file_pointer_; //holds the FILE pointer to the file itself
然後我告訴eclipse生成getter和setter,我得到了這個有趣的結果。
const FILE*& getSequenceFilePointer() const;
我想我知道指針地址和常量的所有信息之後,我就看到它們給了我這個。我理解左邊的常量,它會返回一個常量文件指針,但我不明白右邊的常數以及FILE*getSequenceFilePointer()之間的和號。任何幫助?const FILE *&getSequenceFilePointer()const; ECLIPSE

class Configuration { 
public: 
    const FILE*& getSequenceFilePointer() const; 
private: 
    FILE *sequence_file_pointer_; //holds the FILE pointer to the file itself 
} 
+0

右邊的const使它成爲一個「const成員函數」。該符號稱爲「參考」。在這種情況下,您可以將返回類型更改爲「FILE *'或」const FILE *' – 2014-09-22 19:20:32

+0

請參閱http://stackoverflow.com/questions/751681/meaning-of-const-last-in-ac-method-後綴「const」的聲明。 – juanchopanza 2014-09-22 19:20:49

+0

你正在使用的工具是getter/setter生成工具,實質上是模板化的腦死亡。它只需要成員變量的類型('FILE *'),然後生成一個const-「getter」,它返回所述變量的const-reference-to-type類型。這是否適合您的需求取決於您(如果是這樣,我會發現它很奇怪,因爲它本質上爲您提供了對「FILE *」的引用,您實際上無法使用它)。 – WhozCraig 2014-09-22 19:43:15

回答

0

他們教我用符號,如果我只需要一個參考,而不是一個副本,但僅用於類/結構。當你說你想要返回一個類,一個int或者其他東西時,它會被複制到堆棧中,並且你可以在另一個函數中得到它。但是如果你只想給它一個引用,例如你沒有創建需要在函數內返回的對象,那麼你只傳遞4或8個字節,所以你不必複製一堆內存。

所以這有點奇怪。這似乎是一個錯誤。指針已經是原子的(我認爲它是unsigned long int)。它不需要傳遞引用,因爲引用的大小與它指向的大小相同。這只是浪費時間和記憶。