2013-10-20 46 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)。