[英]What does this construction mean: bool operator == (const a& other) const;?
I have a structure defined in the following way: 我有以下方式定义的结构:
struct struct_name
{
int x;
region y;
bool operator == (const struct_name& other) const;
};
I do not understand the last line in the body of the structure. 我不了解该结构主体的最后一行。 What does it do?
它有什么作用?
Declares operator==
for this struct
. 为该
struct
声明operator==
。 This operator will allow you to compare struct objects in an intuitive way: 此运算符将使您以直观的方式比较结构对象:
struct_name a;
struct_name b;
if( a == b )
// ...
bool operator == (const struct_name& other) const;
^^^^ ^^^^^............^ ^^^^^-- the method is `const` 1
^^^^ ^^^^^............^
^^^^ ^^^^^............^--- the `other` is passed as const reference
^^^^
^^^^-- normally, return true, if `*this` is the same as `other`
1
- this means, that the method does not change any members 1
这表示该方法不会更改任何成员
EDIT: Note, that in C++
, the only difference between class
and struct
is the default access and default type of inheritance (noted by @AlokSave) - public
for struct
and private
for class
. 编辑:请注意,在
C++
,之间的唯一区别class
和struct
是继承默认的访问和默认类型(由@AlokSave说明) - public
的struct
和private
的class
。
It declares a function. 它声明一个函数。 The function's name is
operator==
. 该函数的名称为
operator==
。 It returns bool
and takes a single argument of type const struct_name&
. 它返回
bool
并接受const struct_name&
类型的单个参数。 It final const
in the line says that it is a const member function, which means that it doesn't modify the state of the struct_name
object it is called on. 这行最后的
const
表示它是const成员函数,这意味着它不会修改调用它的struct_name
对象的状态。
This is known as operator overloading . 这称为运算符重载 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.