2013-10-20 43 views
-3

我真的很新的C++和我有一個非常簡單的問題,C++類和對象

struct triplet { 
int first; 
int second; 
int third; 
}; 


class mystery { 

private: 
int x; 
int y; 
struct triplet* the_triplet; 
public: 
mystery(int f, int s, int t); 
~mystery(); 
mystery & mystery_member(const mystery & other) const; 

}; 

什麼行

mystery & mystery_member(const mystery & other) const; 

意味着或做?

+3

它在mystery上聲明一個const非靜態成員函數,它需要一個'mystery const&'並返回一個'mystery&'。 – rightfold

+0

那麼&其他代表的究竟是什麼? – Andy

+0

@Andy'&'表示引用作爲參數傳遞而不是副本。 'other'是爲了可讀性。它應該暗示參數在函數中扮演什麼角色。在函數的刪除中不需要它。在函數的實現中,它是可以用來訪問函數體內參數的變量。 – Oswald

回答

3

mystery & mystery_member(const mystery & other) const聲明mystery

  • 花費mystery類型的對象的引用作爲參數(這是mystery & other部分)的成員函數,
  • 不允許修改該對象(這就是在mystery & other一部分)的前面的const
  • 返回到mystery類型的對象(這是在開始時的mystery &部分)的引用,
  • 不會更改調用它的對象的任何成員變量(最後是const)。