const Point operator + (Point& other) const { return Point(x + other.x, y + other.y); }怎么理解
时间: 2024-04-07 20:27:54 浏览: 21
这是一个类的成员函数,重载了加号运算符,用于实现点(Point)对象之间的加法操作。
该函数返回一个 const Point 类型的对象,在函数体中,使用了一个匿名对象 Point(x + other.x, y + other.y),并将其返回。这个匿名对象的坐标分别为原对象的 x 坐标加上参数对象 other 的 x 坐标,以及原对象的 y 坐标加上参数对象 other 的 y 坐标。
需要注意的是,该函数中的参数是一个非 const 的引用,这允许参数对象被修改,但是原对象是 const 的,不能被修改。因此,该函数适用于常量对象和非常量对象之间的加法操作,但不能修改原对象。
相关问题
USTRUCT(BlueprintType) struct MPBASE_API FGeoCorners { GENERATED_BODY() UPROPERTY(EditAnywhere, Category = "GeoTypes | Corners") FGeoPosition UpLeft; UPROPERTY(EditAnywhere, Category = "GeoTypes | Corners") FGeoPosition UpRight; UPROPERTY(EditAnywhere, Category = "GeoTypes | Corners") FGeoPosition DownLeft; UPROPERTY(EditAnywhere, Category = "GeoTypes | Corners") FGeoPosition DownRight; FGeoBoundingBox ToBoundingBox() const; }; USTRUCT(BlueprintType) struct MPBASE_API FGeoBoundingBox { GENERATED_BODY() UPROPERTY(EditAnywhere, Category = "GeoTypes | BoundingBox") FGeoPosition MinLocation; UPROPERTY(EditAnywhere, Category = "GeoTypes | BoundingBox") FGeoPosition MaxLocation; FGeoBoundingBox() = default; FGeoBoundingBox(double minx, double miny, double maxx, double maxy) : MinLocation(minx, miny), MaxLocation(maxx, maxy) { } FGeoBoundingBox(const FGeoPosition& minLoc, const FGeoPosition& maxLoc) : MinLocation(minLoc), MaxLocation(maxLoc) { } bool IsValid() const { return MinLocation.IsValid() && MaxLocation.IsValid() && MinLocation <= MaxLocation; } bool Contains(const FGeoPosition& location) const { return location.Longitude >= MinLocation.Longitude && location.Latitude >= MinLocation.Latitude && location.Longitude <= MaxLocation.Longitude && location.Latitude <= MaxLocation.Latitude; } FGeoBoundingBox& operator+=(const FGeoPosition& Location); FGeoBoundingBox& operator+=(const FGeoBoundingBox& GeoBox); bool Intersect(const FGeoBoundingBox& Other) const; bool Contains(const FGeoBoundingBox& Other) const; FGeoBoundingBox Intersection(const FGeoBoundingBox& Other) const; bool IsInside(const FGeoPosition & TestPoint) const { return ((TestPoint.Longitude > MinLocation.Longitude) && (TestPoint.Longitude < MaxLocation.Longitude) && (TestPoint.Latitude > MinLocation.Latitude) && (TestPoint.Latitude < MaxLocation.Latitude)); } bool IsInside(const FGeoBoundingBox& Other) const { return (IsInside(Other.MinLocation) && IsInside(Other.MaxLocation)); } FGeoPosition GetCenter() const; FGeoCorners ToCorners() const; double GetDeltaLongitude() const { return MaxLocation.Longitude - MinLocation.Longitude; } double GetDeltaLatitude() const { return MaxLocation.Latitude - MinLocation.Latitude; } };代码含义
这是一段 Unreal Engine 代码,定义了两个结构体:`FGeoCorners` 和 `FGeoBoundingBox`。
`FGeoCorners` 结构体包含了四个属性:`UpLeft`,`UpRight`,`DownLeft`,`DownRight`,这些属性是类型为 `FGeoPosition` 的结构体。它还包含一个名为 `ToBoundingBox` 的函数,用于转换为 `FGeoBoundingBox` 类型的结构体。
`FGeoBoundingBox` 结构体包含了两个属性:`MinLocation` 和 `MaxLocation`,这些属性也是类型为 `FGeoPosition` 的结构体。它还包含了两个构造函数,分别是默认构造函数和带参数的构造函数。
实现一个Point类,该类拥有私有数据成员x和y,要求用运算符重载的方式实现以下功能: 1)前缀自增(即把x和y的值均加1)。 2)大于运算(x * x + y * y的值大者为大)。 3)加法运算(两个点的加法为x和y对应相加)。 再写一段测试代码,测试类Point的功能。
以下是Point类的实现代码:
```cpp
class Point {
private:
int x;
int y;
public:
Point(int a, int b) : x(a), y(b) {}
Point& operator++() {
++x;
++y;
return *this;
}
bool operator>(const Point& other) const {
return x * x + y * y > other.x * other.x + other.y * other.y;
}
Point operator+(const Point& other) const {
return Point(x + other.x, y + other.y);
}
};
```
以下是测试代码:
```cpp
int main() {
Point p1(3, 4);
Point p2(1, 2);
// test prefix increment
++p1;
cout << p1.x << ", " << p1.y << endl;
// test greater than operator
cout << (p1 > p2) << endl;
// test addition operator
Point p3 = p1 + p2;
cout << p3.x << ", " << p3.y << endl;
return 0;
}
```
输出结果:
```
4, 5
1
5, 7
```
说明Point类的功能都能够正常运行。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)